即使在swift手机重启后,如何在后台获取位置? [英] How to get location in background even after phone restart in swift?

查看:134
本文介绍了即使在swift手机重启后,如何在后台获取位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它按照用户选择的时间间隔跟踪手机的位置。我使用计时器启动 startUpdatingLocation ,当它获得位置停止更新时,将位置发送到服务器并再次启动计时器。一切都在后台完成。
我需要在后台模式下(在iOS 8中)获取位置数据,即使在手机重启并将数据发送到服务器之后也是如此。我尝试了数百种方法,没有人适合我。那么......我现在得到的是:
在info.plist中:

I'm developing an application which is tracking the location of the phone on time interval chosen by the user. I made it with timer which starts startUpdatingLocation, when it get the location stops updating sends the location to the server and starts timer again. Everything is done in background. I need to get location data in background mode (in iOS 8) even after phone restarts and send the data to a server. I tried hundreds methods and no one works for me. So ... what i've got at this moment: In info.plist:


  • 所需的背景模式 - 位置和fetch

  • NSLocationAlwaysUsageDescription - 此应用程序需要位置才能正常工作

在AppDelegate中:

In AppDelegate:

    var locationController: LocationController = LocationController() as LocationController;

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

    UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);

    var bgTask = UIBackgroundTaskIdentifier()
    bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
        UIApplication.sharedApplication().endBackgroundTask(bgTask)
    }

    if(self.locationController.profileForTracking != nil && self.locationController.profileForTracking != "Disabled" && self.locationController.intervalForTracking != nil && self.locationController.trackingAllowed == true){
        self.locationController.initLocationManager();

        if(self.locationController.timer != nil){
            self.locationController.timer = self.locationController.timer;
        } else {
            self.locationController.startTimerForLocationUpdate();
        }

        println("Location can now start ....");

    }


    return true
}

func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {

    var bgTask = UIBackgroundTaskIdentifier()
    bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
        UIApplication.sharedApplication().endBackgroundTask(bgTask)
    }

    if(self.locationController.profileForTracking != nil && self.locationController.profileForTracking != "Disabled" && self.locationController.intervalForTracking != nil && self.locationController.trackingAllowed == true){
        self.locationController.initLocationManager();

        if(self.locationController.timer != nil){
            self.locationController.timer = self.locationController.timer;
        } else {
            self.locationController.startTimerForLocationUpdate();
        }

        println("Location can now start ....");

    }
}

一切似乎都有效(当应用时)除非重新启动电话或用户终止应用程序,否则处于前台或后台/非活动状态。我认为我做错了什么,iOS不会唤醒应用程序。我怎么能这样做,如果我能以某种方式做到这一点,那么就可以获得用户选择的时间段(30分钟,1小时......)。在此先感谢!

Everything seems to work (when app is in foreground or background/inactive) except when the phone was restarted or user terminate application. I think I'm doing something wrong and iOS does not wake up the application. How can I do it, and if I can do it somehow is there a way so get the location in time period chosen by the user (30 minutes, 1 hour ...). Thanks in advance!

推荐答案

在移至后台或应用终止时调用此方法

While moving to background or when app terminated call this method

startMonitoringSignificantLocationChanges();

因此,如果您重启设备或应用程序被杀,只要您的位置发生重大位置变更,操作系统将在后台启动您的应用。

So if you reboot your device or app got killed, whenever there is significant location change in your location, OS will launch your app in background.

根据苹果文档


如果您启动此服务且您的应用是随后终止,
系统会在新的
事件到来时自动将应用重新启动到后台。在这种情况下,选项字典传递给
应用程序:willFinishLaunchingWithOptions:和
应用程序:didFinishLaunchingWithOptions:你的应用程序的方法
委托包含密钥UIApplicationLaunchOptionsLocationKey到
表示你​​的应用程序由于位置事件而启动。在
重新启动时,您仍然必须配置位置管理器对象并调用
此方法以继续接收位置事件。当您重新启动
位置服务时,当前事件会立即传递给您的代理
。此外,在开始提供位置服务之前,您的位置
经理对象的位置属性将填充最新的位置对象,甚至是

If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

这篇关于即使在swift手机重启后,如何在后台获取位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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