即使应用程序在 ios 中被终止或从后台删除,我如何继续更新我的位置?

How Can I Continue updating my location even when app is killed or removed from background in ios?

我的应用程序需要不断更新位置,即使应用程序被终止或从后台删除也是如此。它在前台工作正常,甚至在后台 mode.but 应用程序被杀死时也无法正常工作。 我已经尝试了一些代码。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.startLocationService()

    if ((launchOptions?[UIApplicationLaunchOptionsLocationKey]) != nil) {
       self.locationmanager = CLLocationManager()
        self.startLocationService()
    }

    print("Location Updates started")


    return true
}

func startLocationService()
{
    self.locationmanager.requestWhenInUseAuthorization()
    self.locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationmanager.allowsBackgroundLocationUpdates = true
    self.locationmanager.requestAlwaysAuthorization()
    self.locationmanager.delegate = self


    if UIApplication.sharedApplication().backgroundRefreshStatus == .Available {
        print("Background updates are available for the app.")

    }
    else if UIApplication.sharedApplication().backgroundRefreshStatus == .Denied {
        print("The user explicitly disabled background behavior for this app or for the whole system.")

    }
    else if UIApplication.sharedApplication().backgroundRefreshStatus == .Restricted {
        print("Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.")

    }
    else
    {
        print("nothing")
    }

    self.locationmanager.startUpdatingLocation()
}



func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   // locationManager = CLLocationManager()
        }

  func applicationDidBecomeActive(application: UIApplication) {

    self.startLocationService()
}

func applicationWillTerminate(application: UIApplication) {      
             self.locationmanager.startMonitoringSignificantLocationChanges()

}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     print("did update locateion called-->..")
    let location:CLLocation = locations[locations.count-1] 

    print(location.coordinate.longitude)
      print(location.coordinate.latitude)

    let newTime : NSDate = NSDate()

    let calendar: NSCalendar = NSCalendar.currentCalendar()
    let flags = NSCalendarUnit.Second
    let components = calendar.components(flags, fromDate: oldTime, toDate: newTime, options: [])


   //Update locations to server 
    self.call_service_insertLocation("location", loc: location)

}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print(error)
}

这是可能的,但你必须跳过几个环节。 被杀死时发送位置更新的唯一方法是使用区域监控 (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html)。 设置时,您的应用程序将由 OS 打开,然后您有几秒钟的时间来处理信息,然后应用程序被终止。有足够的时间将位置更新发送到您的 server/local 存储。 还有一个API叫做CLVisit,但是完全由操作系统控制,不可靠。

希望对您有所帮助。 您的应用程序可以在由于某种原因(内存压力)终止时被唤醒。

但是当应用程序终止时,后台位置更新有限制。

这是来自 Apple 的位置和地图编程指南的注释。

If your app is terminated either by a user or by the system, the system doesn't automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or significant-change location service. However, when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn't relaunch your app for any location events, including significant change or region monitoring events. Further, while Background App Refresh is off your app won't receive significant change or region monitoring events even when it's in the foreground.

因此,要在后台接收位置更新,您应该为您的应用开启 Background App Refresh 设置(您可以在 iPhone 的 Settings/Your App/Background App Refresh 中查看该设置。)

此外,只有在应用程序终止时才会交付重大更改,并且像您在此处提到的位置更改(我的意思是 kCLLocationAccuracyBest)可能不会唤醒您的应用程序。 此外,您应该在您的应用委托中重新启动位置管理器 s application:didFinishLaunching:withOptions 方法来检索下一个重要的位置变化。还要确保在后台模式下检索位置时尽快 return 或使用后台任务机制使应用程序在后台工作几分钟。 (2~3 分钟).