简体   繁体   English

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

[英]After killing the app from the background, app should run gps tracking application in ios (objective c)

I am making a GPS tracking application. 我正在制作GPS跟踪应用程序。 The app should track the iPhone user location. 该应用程序应跟踪iPhone用户位置。 In my case it tracking when app is in background and open . 在我的情况下,它跟踪应用程序在后台和打开时。 if I am killing my app from background I am not getting location update. 如果我从后台杀死我的应用程序,我没有获得位置更新。 Is this an any possible after killing the app, app should track the location in iOS(objective c). 在杀死应用程序后这是一个可能的应用程序,app应该跟踪iOS中的位置(目标c)。

There is a way to get the location update even when the app is killed/terminated by the user or iOS. 即使应用程序被用户或iOS终止/终止,也有一种方法可以获取位置更新。

In iOS 8 and iOS7 在iOS 8和iOS7中

use [locationManager startMonitoringSignificantLocationChanges] instead of [ locationManager startUpdatingLocation] please check this http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended . 使用[locationManager startMonitoringSignificantLocationChanges]而不是[ locationManager startUpdatingLocation]请查看http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

A sample project also in git pls go through that too https://github.com/voyage11/GettingLocationWhenSuspended 同样在git中的示例项目也可以通过它来https://github.com/voyage11/GettingLocationWhenSuspended

Add location updates to your application capabibities in settings: 在设置中为您的应用程序功能添加位置更新:

Image : Setting capabilities to Location updates 图像:设置位置更新的功能

Then, add Privacy policy description for location usage set to location always usage description: 然后,将位置使用设置的隐私策略说明添加到位置始终使用说明:

Image : Location Always usage description in plist 图像:位置始终在plist中使用说明

Finally, add this piece of code in appdelegate: 最后,在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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM