我怎样才能继续更新我的位置,即使应用程序被杀死或从背景中删除ios?

我的应用程序需要不断更新位置,即使应用程序被杀死或从后台删除。 它在前台甚至后台模式下都能正常工作,但在应用程序被杀时不工作。 我试过一些代码。

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) } 

我希望这可以帮助你。 由于某种原因(内存压力),应用程序可能会被唤醒。

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

这是来自苹果的位置和地图编程指南。

如果您的应用程序被用户或系统终止,则新的位置更新到达时系统不会自动重新启动您的应用程序。 用户必须在传送位置更新恢复之前,明确重新启动您的应用程序。 自动重新启动应用程序的唯一方法是使用区域监视重大更改位置服务 。 但是,当用户全局或专门为您的应用禁用“ 后台应用刷新”设置时,系统不会针对任何位置事件(包括重大更改或区域监视事件)重新启动您的应用。 此外,当后台应用程序刷新closures时,即使应用程序处于前台,您的应用程序也不会收到重大更改或区域监视事件。

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

也只有当应用程序被终止,像你在这里提到的位置更改(我的意思是kCLLocationAccuracyBest )可能不会唤醒你的应用程序将发送重大更改。 此外,您应该在应用程序委托application:didFinishLaunching:withOptions重新启动位置pipe理器application:didFinishLaunching:withOptions方法来检索下一个重要的位置更改。 还要确保在后台模式下检索位置时尽快返回,或使用后台任务机制使应用程序在后台工作几分钟。 (2〜3分钟)。

它可能,但你必须跳过几个篮球。 杀死时发送位置更新的唯一方法是使用区域监视( https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html )。 安装完成后,您的应用程序将被操作系统打开,然后在应用程序被终止之前,您有几秒钟的时间来处理信息。 它有足够的时间将位置更新发送到您的服务器/本地存储。 另外还有一个名为CLVisit的API,但是它完全由操作系统控制,不可靠。