从后台杀死应用程序后,应用程序应在ios中运行GPS跟踪应用程序(目标c)



我正在制作一个GPS跟踪应用程序。该应用程序应跟踪iPhone用户的位置。就我而言,它会跟踪应用程序何时在后台并打开。如果我从后台杀死我的应用程序,我没有得到位置更新。杀死应用程序后,这是否可能,应用程序应该在iOS中跟踪位置(目标c)。

一种方法可以获取位置更新,即使应用程序被用户或iOS杀死/终止。

在 iOS 8 和 iOS7 中

使用 [locationManager startMonitoringSignificantLocationChanges] 代替 [ locationManager startUpdatingLocation]请检查此 http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended。

同样在 git 中的示例项目也请经历 https://github.com/voyage11/GettingLocationWhenSuspended

在设置中将位置更新添加到应用程序功能:

图像 :将功能设置为位置更新

然后,将位置使用情况的隐私策略说明设置为"位置始终使用情况说明":

图片 : 位置 始终 plist 中的使用说明

最后,在appdelegate中添加这段代码:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
           [self.locationManager requestAlwaysAuthorization];
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager startMonitoringSignificantLocationChanges];
        } else {
            [self.locationManager startUpdatingLocation];
        }
return YES;
}
(void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    [locationManager stopUpdatingLocation];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    [locationManager startUpdatingLocation];
}

(void)applicationDidEnterBackground:(UIApplication *)application {
    [locationManager stopUpdatingLocation];
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
    }];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                      target:self
                                                    selector:@selector(startTrackingBg)
                                                    userInfo:nil
                                                     repeats:YES];

}
(void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
        NSLog(@"App terminated");
    }];
}
(void)startTrackingBg {
    [locationManager startUpdatingLocation];
    NSLog(@"App is running in background");
}

最新更新