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

百般思念 提交于 2019-12-13 17:23:31

问题


I am making a GPS tracking application. The app should track the iPhone user location. 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).


回答1:


There is a way to get the location update even when the app is killed/terminated by the user or iOS.

In iOS 8 and 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.

A sample project also in git pls go through that too https://github.com/voyage11/GettingLocationWhenSuspended




回答2:


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

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


来源:https://stackoverflow.com/questions/41844860/after-killing-the-app-from-the-background-app-should-run-gps-tracking-applicati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!