即使应用程序被终止或终止,位置也会更新

Kir*_*rti 7 location core-location ios swift

我甚至在所有州都尝试获取位置更新,即使应用程序被终止/终止/暂停也是如此.我在xcode中启用了后台提取并实现了以下代码(使用了参考" 在所有状态app中捕获位置 ").但是,当我终止应用程序时,它会在AppDelegate类上给出一条红线.我不明白这里有什么问题.我已经使用" 获取iOS应用程序的位置,当它在后台甚至被杀 " 这个问题的解决方案,但它不适用于ios 9.请帮助我出或告诉我其他解决方案.

更新的代码 -

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var window: UIWindow?
var locationManager: CLLocationManager?
var significatLocationManager : CLLocationManager?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){
        print("yessssss")
    }else{
        print("noooooo")
    }

    if let launchOpt = launchOptions{
        if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) {
            self.significatLocationManager = CLLocationManager()
            self.significatLocationManager?.delegate = self
            self.significatLocationManager?.requestAlwaysAuthorization()
            if #available(iOS 9.0, *) {
                self.significatLocationManager!.allowsBackgroundLocationUpdates = true
            }
            self.significatLocationManager?.startMonitoringSignificantLocationChanges()

        }else{

            self.locationManager           = CLLocationManager()
            self.locationManager?.delegate = self
            self.locationManager?.requestAlwaysAuthorization()
            if #available(iOS 9.0, *) {
                self.locationManager!.allowsBackgroundLocationUpdates = true
            }

            self.locationManager?.startMonitoringSignificantLocationChanges()
        }

    }else{

        self.locationManager           = CLLocationManager()
        self.locationManager?.delegate = self
        self.locationManager?.requestAlwaysAuthorization()
        if #available(iOS 9.0, *) {
            self.locationManager!.allowsBackgroundLocationUpdates = true
        }

        self.locationManager?.startMonitoringSignificantLocationChanges()

    }

    return true
}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

    let locationArray = locations as NSArray
    let locationObj = locationArray.lastObject as! CLLocation
    let coord = locationObj.coordinate
        }


func applicationDidEnterBackground(application: UIApplication) {

    if self.significatLocationManager != nil {

        self.significatLocationManager?.startMonitoringSignificantLocationChanges()
    }else{

        self.locationManager?.startMonitoringSignificantLocationChanges()
    }


}
Run Code Online (Sandbox Code Playgroud)

Sal*_*tan 2

检查本教程: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedtermeratedsuspended

源代码在这里: https: //github.com/voyage11/GettingLocationWhenSuspending

希望您能得到答案。它对我有用。现在我尝试在调用“didUpdateLocations”函数时发出http请求,以便在应用程序未运行(挂起/终止/终止)时可以将用户当前位置存储在服务器中。

我不认为苹果允许在应用程序暂停/终止时发出任何http请求。但如果服务器收到位置更新的请求,那么我很乐意接受。因为我不需要服务器的响应。需要大量的测试......