我正在开发一个位置跟踪应用。用户可以开始跟踪,做任何他想做的事情(将应用程序放在后台,锁定手机等),返回应用程序并停止跟踪。
如果应用程序被系统或用户杀死,我希望跟踪重启。为此,我在文档中看到我必须使用重要的位置更改服务,但此服务不会发送足够的位置。由于重要的位置变更服务,重启应用程序时是否可以重新启动标准位置服务?或者应用程序会被拒绝吗?
答案 0 :(得分:0)
即使在后台被杀,以下方法也会唤醒您的应用程序:
您的应用程序将使用applicationDidFinishLaunchingWithOptions
方法中的位置键启动。以下是如何优雅地处理它的示例(来自http://nshipster.com/launch-options/):
// .h
@import CoreLocation;
@interface AppDelegate () <CLLocationManagerDelegate>
@property (readwrite, nonatomic, strong) CLLocationManager *locationManager;
@end
// .m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
if (![CLLocationManager locationServicesEnabled]) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Location Services Disabled", nil)
message:NSLocalizedString(@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled.", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil] show];
} else {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
}
if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {
[self.locationManager startUpdatingLocation];
}
}
此外,您不仅限于使用重要的位置监控或区域监控等。您可以针对不同的用例使用不同的机制。例如。您可以使用区域监控来设置用户周围的回退区域,每次发生退出事件时,您的应用程序将被location
密钥唤醒。然后,您可以重新启动位置服务