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

标签 ios swift background location cllocationmanager

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

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

最佳答案

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

但是应用程序终止时后台位置更新存在限制。

这是 Apple 位置和 map 编程指南中的注释。

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.

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

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

关于ios - 即使应用程序被终止或从 ios 后台删除,我如何才能继续更新我的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343033/

相关文章:

swift - 如何在 macOS 的登录项中显示应用程序助手?

swift - 了解 CoreData 的错误处理

html - 背景的绝对位置

手机处于 sleep 模式时,android O 未收到 FCM 推送消息

ios - 通过 Parse.com 下载将 NSData 转换为 UIImage 时出现问题

ios - iOS 在显示之前如何设置 ViewController View 框架?

iOS: Storyboard转场

iphone - XML字符串中带有特殊字符的发布请求

ios - 标签背景颜色根据来自服务器的文本更改

javascript - 如何在这个随机背景js中连续两次不出现相同的bg