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

Posted

技术标签:

【中文标题】即使应用程序在 ios 中被杀死或从后台删除,如何继续更新我的位置?【英文标题】:How Can I Continue updating my location even when app is killed or removed from background in ios? 【发布时间】:2017-01-13 13:24:36 【问题描述】:

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

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)

【问题讨论】:

也许this 会有所帮助 您好,请在App的功能中开启Background Refresh 嘿@Tj3n 我也试过了。它在 ios9 中不起作用,还有其他建议吗? @panktipatel 这里是一个很好的演示,用于在应用程序终止时获取位置mobileoop.com/… 【参考方案1】:

我希望这可以帮助你。 您的应用可以在由于某种原因(内存压力)终止时被唤醒。

但应用终止时后台位置更新有限制。

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

如果您的应用被用户或系统终止,系统不会在新的位置更新到来时自动重启您的应用。用户必须在位置更新恢复交付之前明确地重新启动您的应用程序。让您的应用自动重新启动的唯一方法是使用区域监控重大变化位置服务。 但是,当用户全局或专门为您的应用禁用 后台应用刷新 设置时,系统不会针对任何位置事件(包括重大变化或区域监控事件)重新启动您的应用。 此外,当后台应用刷新关闭时,您的应用即使在前台也不会收到重大更改或区域监控事件。

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

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于即使应用程序在 ios 中被杀死或从后台删除,如何继续更新我的位置?的主要内容,如果未能解决你的问题,请参考以下文章

为啥即使我在后台请求位置更新,我的 iOS 应用程序也会被杀死?

即使App被IOS中的用户杀死,如何每10秒调用一次api?

即使在应用程序被杀死后如何继续进行 iOS 位置跟踪?

Android 警报管理器在重新启动设备或从后台杀死应用程序后取消

即使应用程序处于终止状态或被杀死,如何在 iOS 中获取位置更新? [复制]

后台服务在android中被杀死