【发布时间】:2013-02-21 03:04:02
【问题描述】:
在 iPhone 应用程序中,即使有权定位的应用程序没有在后台运行,是否也可以跟踪位置并将其发送到服务器。
【问题讨论】:
-
我没有答案,但我可以肯定地说这是可能的,只是您可能无法将该应用程序提交到应用商店。
标签: iphone ios objective-c ios5 ios6
在 iPhone 应用程序中,即使有权定位的应用程序没有在后台运行,是否也可以跟踪位置并将其发送到服务器。
【问题讨论】:
标签: iphone ios objective-c ios5 ios6
这是可能的——请参阅this document 了解一般的多任务处理和此section of the Location Awareness Programming Guide for "Getting Location Events in the Background"。当然,所有这些都是关于 iOS 设备获取您位置的所有各种方式(蜂窝塔三角测量、Skyhook 式 wifi 网络观测和 GPS),而不是唯一的 GPS。
简而言之,通过阅读这些文档:将 UIBackgroundModes 键添加到您的 info.plist(一个数组)中,并将值“位置”放入其中。即使在后台,您也会收到 CLLocationManager 更新。
但是,如果您想对电池友好,那么您最好在 CLLocationManager 上使用 startMonitoringSignificantLocationChanges 方法。然后,即使在后台没有完整的后台应用程序,您也可以获得适当的重要位置更新。文档的其他部分指出,重大变化是从一个蜂窝塔到另一个蜂窝塔的任何变化。
【讨论】:
如果App在后台,可以在info.plist中添加键值对。键是UIBackgroundModes,值如下:
然后在后台做点什么:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 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.
UIDevice *device = [UIDevicecurrentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
backgroundSupported = YES;
}
__blockUIBackgroundTaskIdentifier bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTaskId];
bgTaskId = UIBackgroundTaskInvalid;
}];
if (backgroundSupported) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
});
}
}
但如果应用程序甚至不在后台,即不在内存中,那么应用程序可以做什么? CPU不会运行应用程序的一行代码。
【讨论】: