【问题标题】:Standard Location based iOS app not waking up after suspended IOS基于标准位置的 iOS 应用程序在暂停 IOS 后未唤醒
【发布时间】:2016-02-22 13:34:41
【问题描述】:

我正在开发一个基于位置的应用程序,它应该总是获取用户位置。我使用标准位置服务。但问题是,即使我们移动到其他位置,应用程序在后台保持空闲一段时间后也不会获取坐标。根据苹果文档,当一个新位置到达时,应用程序应该会自动唤醒,但这里不会发生这种情况。我正在共享代码并用于获取我的 plist 的位置和屏幕截图。

  class SALocation: NSObject,CLLocationManagerDelegate
{
    static let sharedInstance : SALocation = SALocation()

    var locationManager : CLLocationManager!
    var location : CLLocation!
    var address : String!
    var latitude : NSString?
    var longitude : NSString?
    var isAdderssLoaded : Bool = false
    var locdictionary : NSMutableDictionary = NSMutableDictionary()

    func startLocationManager()
    {
        if self.locationManager == nil
        {
            self.locationManager = CLLocationManager()

            if CLLocationManager.locationServicesEnabled(){
                print("location service enabled")
            }
            self.locationManager.delegate = self
            self.locationManager.requestWhenInUseAuthorization()
            self.locationManager.distanceFilter = kCLDistanceFilterNone
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

            if ( Float (UIDevice.currentDevice().systemVersion) >= 9) {
                if #available(iOS 9.0, *) {
                    self.locationManager.allowsBackgroundLocationUpdates = true
                } else {
                    // Fallback on earlier versions
                };

            }

            self.locationManager.startUpdatingLocation()
            //self.locationManager.stopMonitoringSignificantLocationChanges()
        }
        else
        {
            self.locationManager.startUpdatingLocation()
        }
    }

    // MARK: CLLocationManagerDelegate
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        UIAlertView(title:"Alert", message:error.description, delegate: nil, cancelButtonTitle:nil, otherButtonTitles:"Ok").show()
    }
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        if locations.count > 0
        {
            self.location = locations[0]
            /* storing date and location to plist 

            */

            let datenow = NSDate()
            let dateformatternow = NSDateFormatter ()
            dateformatternow.dateFormat = "yyyyMMdd HH:mm:ss"
            let timenow:NSString = dateformatternow.stringFromDate(datenow)
            let documetsdirectorypath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last
            latitude = NSString(format: "%f",self.location.coordinate.latitude)
            longitude = NSString (format: "%f",self.location.coordinate.longitude)
            let latlong : NSString = NSString(format:"%@~%@",latitude!,longitude!)
            NSUserDefaults.standardUserDefaults().setObject(latlong, forKey: "latlong")
            let aFilePath =  NSString(format: "%@/location.plist",documetsdirectorypath!)
            locdictionary.setObject(latlong, forKey: timenow as String)
            locdictionary.writeToFile(aFilePath as String, atomically: true)
            ///////////// ||storing date and location to plist code ends here||\\\\\\





           // self.getAddressFromLocation(locations[0] )
//            if (NSUserDefaults.standardUserDefaults().objectForKey(SettingAppRefresh) != nil)
//            {
//                if (NSUserDefaults.standardUserDefaults().objectForKey(SettingAppRefresh) as! NSString).isEqualToString(FalseString)
//                {
//                   // self.locationManager.stopUpdatingLocation()
//                }
//            }
        }
    }

  }

我在这里所做的只是获取位置并将其写入 plist 文件。这适用于前景,背景等。但是当我让应用程序闲置 20 分钟时,即使我在应用程序暂停时移动到其他一些位置,也不会获取位置

功能选项卡看起来像这样

【问题讨论】:

    标签: ios core-location cllocationmanager background-process


    【解决方案1】:

    要在后台启动位置,您必须从以下路径启动后台服务

    点击你的名字 -> 点击你的应用名称(目标) -> 转到功能 -> 找到后台模式 -> 启用位置更新模式

    我不确定你是否开始这样做,因为你没有放任何关于这个的截图。

    并检查您的用户是否在设置中启动了后台刷新。请参阅下面的链接。

    Background App Refresh checking, enabling and disabling programatically for whole device and for each particular application in iOS 7

    更新:: 用于背景中的位置更新链接(目标 c)

    http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/

    【讨论】:

    • 我已经这样做了,唯一的问题是设备保持空闲一段时间使应用程序暂停并停止获取位置。即使我们移动到其他位置应用程序仍然暂停。如何唤醒应用程序?我打算做的一些事情是显着改变位置服务......
    【解决方案2】:

    好吧,我不知道您是如何获取位置更新的 - 例如重大位置变化以及您如何从后台退出。 我建议检查您的应用程序是否真的处于后台模式 - UIApplication.sharedApplication().applicationState 因为它可以被终止。 我还建议查看 Apple 的 Execution States for Apps。 - 特别是对于您可能的用例实现长时间运行的任务部分。 rayywenderlich.com 上还有一个很好的教程,称为背景模式。

    【讨论】:

      【解决方案3】:

      请使用

      self.locationManager.requestAlwaysAuthorization()

      不要忘记更新您的 Info.plist 以定义 NSLocationAlwaysUsageDescription 键。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        相关资源
        最近更新 更多