在iOS的用户的位置的认识背景追踪 [英] Understanding background tracking of user's location in iOS

查看:196
本文介绍了在iOS的用户的位置的认识背景追踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保持获取用户的位置更新应用程序时,将背景,正如我苹果的文档中找到,这是被允许执行长期运行的后台任务之一。我还需要作一些处理和网络通话时的位置更新通知发送给服务器的一些信息。我试图做的,在广招,是这样的:

I need to keep getting user's location updates when app going to background and, as I found in Apple's documentation, this is one of the long-running background tasks that are allowed to be implemented. I also need to make some processing and network calls to send to server some info when location updates are notified. What I'm trying to do, in broad strokes, is something like this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
      [application endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
   }];

   // Background task        
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      NSLog(@"Background time remaining: %f seconds (%d mins)",
     [[UIApplication sharedApplication] backgroundTimeRemaining], 
     (int)([[UIApplication sharedApplication] backgroundTimeRemaining] / 60));

      // Need to get data from database here

      // Finished
      if (bgTask != UIBackgroundTaskInvalid) {
         [application endBackgroundTask:bgTask];
         bgTask = UIBackgroundTaskInvalid;
      }

   });

   // Start location manager
   locationManager = [[CLLocationManager alloc] init];
   if ([CLLocationManager locationServicesEnabled]) {          
      locationManager.delegate = self;
      locationManager.distanceFilter = 20;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      [locationManager startUpdatingLocation];
   }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
   }];

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      // Want to perform some data processing here, including concurrent tasks 
      // and several network calls to get and send data to server

      [self locationProcessingCompleted];

   });
}

- (void)locationProcessingCompleted
{
   // Check results. If needed, some more data processing and several network 
   // calls to send data to server

   // Close background task
   if (bgTask != UIBackgroundTaskInvalid) {
      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
   }
}

我不`吨找到有关位置的服务的长期运行的后台任务的任何code的例子,我有这几个问题:

I don`t find any code example of a long-running background task regarding location services, and I have several questions about it:

a)是否有必要执行 dispatch_async 队列中的所有后台code?换句话说,在 applicationDidEnterBackground所有code:异步执行,包括位置管理器设置

a) Is it necessary to execute all the background code within a dispatch_async queue? In other words, has all the code in applicationDidEnterBackground: to be executed asynchronously, including the location manager settings?

B) [UIApplication的sharedApplication] backgroundTimeRemaining] 是返回一个很长的价值。这个时间不应该是10分钟左右?

b) [[UIApplication sharedApplication] backgroundTimeRemaining] is returning a very long value. This time is not supposed to be around 10 minutes?

三)我想做的事:执行初始有限长度的任务,启动位置更新,然后执行一些其他有限长度的任务,每一次 didUpdateToLocation:是触发。我已经设置了位置 UIBackgroundModes 的价值的Info.plist 。是我的方法正确吗?我会继续的位置被无限期更新,然后我可以在每一次更新不到10分钟长的执行有限长度的任务接收?

c) I want to do: perform an initial finite-length task, start location updates, and then perform some other finite-length tasks every time that didUpdateToLocation: is triggered. I have set the location value of UIBackgroundModes in Info.plist. Is my approach correct? Will I keep locations being updating indefinitely, and then can I perform a finite-length task less than 10 min long at every update received?

请,我需要这个问题有所帮助。在此先感谢

Please, I need help with this issues. Thanks in advance

推荐答案

所有你需要做的就是增加应用程序注册了位置更新下的所需的背景模式,您可在文本编辑器或者编辑plist文件,并添加以下code,也可以用x code以添加所需的背景模式。

All you have to do is add "App registers for location updates" under the "Required background modes" you can either edit plist file in a text editor and add following code or can use xcode to add required background modes.

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

在didUpdateToLocation方法将被调用(甚至当你的应用程序是在后台)。您可以在调用此方法的基础执行任何事......

the didUpdateToLocation method will be called (even when your app is in background). You can perform any thing on the bases of this method call...

X $ C $的C快照

Snapshot of Xcode

这篇关于在iOS的用户的位置的认识背景追踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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