iOS的全时基于位置跟踪后台服务 [英] iOS full-time background-service based on location tracking

查看:159
本文介绍了iOS的全时基于位置跟踪后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写这取决于所在的位置跟踪应用程序和有关位置服务器发送数据。但问题是,它具有全天候运行,目前我遇到每2-3天发生的随机崩溃。我做了什么,使应用程序运行在不断的背景是我把的NSTimer在beginBackgroundTaskWithExpirationHandler方法正确iside的applicationDidEnterBackground方法。定时器执行每分钟和停止/启动定位服务。

I'm currently writing an application which depends on location tracking and sending data about the position to the server. The problem, however, is that it has to run 24/7 and currently I'm experiencing random crashes which occur every 2-3 days. What I have done to make the application run constantly in the background is I put a NSTimer in a beginBackgroundTaskWithExpirationHandler method right iside the applicationDidEnterBackground method. The timer executes each minute and stops/starts the location service.

下面是一个示例崩溃日志

在code基本上是这样的:

The code basically looks like this:

UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTaskId = 0;

bgTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
    NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1 * 60.0 target: self selector: @selector(onTick) userInfo: nil repeats: YES];
    [t fire];

    if (bgTaskId != UIBackgroundTaskInvalid){
        [app endBackgroundTask: bgTaskId];

        bgTaskId = UIBackgroundTaskInvalid;
    }
}];

我使用的 GCDAsyncSockets 的用于连接的目的,具有约30秒的超时每次调用。

I am using GCDAsyncSockets for connection purposes, each call having a timeout of approximately 30 seconds.

我真的没了主意,什么可能是发生事故的原因是什么?

I'm really out of ideas, what might be the reason the crashes occur?

推荐答案

您计时器可能触发关闭后的任务失效(后 [UIApplication的sharedApplication] .backgroundTimeRemaining 变为0。

Your timer is probably firing off AFTER the task is invalidated (after [UIApplication sharedApplication].backgroundTimeRemaining gets to 0.

问题是,您不能让应用程序运行的一直在后台的。如果你想在同时执行code每一次,你唯一的选择是将要使用的背景位置API,设置,您的应用使用其plist中的位置的背景模式。
你会得到的 CLLocationManagerDelegate 回调,你必须的一些时间的办时调用这些方法的一些工作。

The thing is that you can't make the application run constantly in the background. If you want to execute code every once in a while, your only option is going to be using the background location API, setting that your app is using the location background mode in its plist. You would be getting the CLLocationManagerDelegate callbacks, and you have some time to do some work when those methods are called.

请参阅有关背景模式的苹果文档:<一href=\"http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html\" rel=\"nofollow\">http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

See the Apple documentation regarding background modes: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

和位置,awarness手册:<一href=\"http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497\" rel=\"nofollow\">http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497

And the location-awarness manual: http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497

这篇关于iOS的全时基于位置跟踪后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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