即使应用程序在ios中被杀死或从后台删除,我该如何继续更新我的位置? [英] How Can I Continue updating my location even when app is killed or removed from background in ios?

查看:93
本文介绍了即使应用程序在ios中被杀死或从后台删除,我该如何继续更新我的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要持续更新位置,即使应用被杀死或从后台移除。它在前台甚至在后台模式下都能正常工作。但是当应用程序被杀死时它无法工作。
我尝试了一些代码。

My app requires to update location continuously, even if app is killed or removed from background. It works fine in foreground and even in background mode.but not working when app is killed. I've tried some code.

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


推荐答案

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

I hope this might help you. Your app can be awaken when it is terminated by some reason (Memory pressure).

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

But there's limitation on background location update when app is terminated.

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

This is note from Apple's Location and Map programming guide.


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

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.

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

So to receive location update in background, you should turn on Background App Refresh setting for your app (You can see the setting in Settings/Your App/Background App Refresh in your iPhone.)

此外,只有在应用程序终止时才会发送重大更改你在这里提到的位置变化(我的意思是 kCLLocationAccuracyBest )可能不会唤醒你的应用程序。
此外,您应该重新启动应用委托中的位置管理器
s 应用程序:didFinishLaunching:withOptions 方法以检索下一个重要位置更改。还要确保在后台模式下检索位置时尽快返回,或使用后台任务机制使应用程序在后台运行更多时间。 (2~3分钟)。

Also only significant changes will be delivered when app is terminated and like location changes you mentioned here (I mean kCLLocationAccuracyBest) probably will not wake your app. Further more, you should restart the location manager in your app delegate s application:didFinishLaunching:withOptions method to retrieve next significant location changes. Also make sure return as soon as possible when location is retrieved in background mode or use background task mechanism to make app working more a few minutes in background. (2~3 mins).

这篇关于即使应用程序在ios中被杀死或从后台删除,我该如何继续更新我的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆