ios - 基于标准位置的 iOS 应用程序在暂停 IOS 后不会唤醒

标签 ios core-location cllocationmanager background-process

我正在开发一个基于位置的应用程序,它应该始终获取用户位置。我正在使用标准位置服务。但问题是,即使我们移动到其他位置,应用程序在后台保持空闲一段时间后也不会获取坐标。根据苹果文档,当到达新位置时,应用程序应该自动唤醒,但这里没有发生这种情况。我正在共享代码并用于获取我的 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 分钟时,即使我移动到其他位置,因为应用程序已暂停,也不会获取位置

enter image description here

“功能”选项卡如下所示 enter image description here

最佳答案

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

Click on your name -> Click on your app name (target) -> goto capabilities -> find the background mode -> enable the location update mode

enter image description here

我不确定您是否启动了该操作,因为您没有提供任何有关此操作的屏幕截图。

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

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

更新:: 对于后台位置更新,请使用以下链接( objective-c )

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

关于ios - 基于标准位置的 iOS 应用程序在暂停 IOS 后不会唤醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820328/

相关文章:

ios - 如何停止在我的 iOS 应用程序中使用定位服务?

ios - Watchkit 扩展时出错

ios - 区分基于MacOS和iOS的pod for macCatalyst (Xcode 11 Beta 5)

ios - 如何通过点击按钮滚动到 ScrollView 中的特定 View ?

ios - 核心位置会在背景出现时给出旧位置

ios - 地理围栏 (CLCircularRegion) 和 iBeacons (CLBeaconRegion) 是否共享 20 个限制?

xcode - Xcode 命令行工具项目是否允许 CoreLocation 权限?

ios - 在 iOS 中加载保存的图像

ios - 在现有 iOS 应用程序中集成 react-native(0.40.0) 后找不到 yoga/Yoga.h 头文件

iOS以编程方式撤销位置服务权限