IOS development and ios development tutorial
Previously, we had a Positioning Project, similar to Didi chuxing. The background must be continuously located.
Baidu map is selected here, but in the background for continuous positioning, the previous simple settings are as follows:
However, after testing, we found that the setting would take about 30 minutes to run in the background and be crash again. When the application is re-opened, the location is automatically restored.
Of course, this is not what we want, so we have struggled to achieve continuous background positioning.
In general, we can use 10 minutes after entering the background to complete some things.
To achieve continuous positioning, every 10 minutes. Automatically enable location again. This solves the problem.
The details are as follows:
AppDelegate. h
@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier bgTask;
AppDelegate. m
-(Void) backgroundHandler {NSLog (@ "###--> backgroundinghandler"); UIApplication * app = [UIApplication sharedApplication]; bgTask = [app handle: ^ {[app endBackgroundTask: bgTask]; bgTask = UIBackgroundTaskInvalid;}]; // Start the long-running task dispatch_async (dispatch_get_global_queue (queue, 0), ^ {// what you want to do, // For example, I am sending a broadcast and reactivate the location. // obtain the globally unique nsicationicationcenter In the ios system. * nc = [nsicationicationcenter defacenter center] // set the broadcast content NSDictionary * dict = [[NSDictionary alloc] init]; // encapsulate the content into the broadcast and send the broadcast to the ios system // LocationTheme channel [nc postNotificationName: @ "LocationTheme" object: self userInfo: dict] ;});}
- (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. BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }]; if (backgroundAccepted) { NSLog(@"backgrounding accepted"); } [self backgroundHandler];}
Then, at the location where positioning is enabled, it becomes the broadcast receiver and reactivate the positioning.
// Initialize BMKLocationService myLocService = [[BMKLocationService alloc] init]; myLocService. delegate = self; // start LocationService [myLocService startUserLocationService];
Nsicationicationcenter * nc2 = [NSNotificationCenter defacenter center]; // call self recvBcast: function [nc2 addObserver: self selector: @ selector (activeLocation :) name: @ "LocationTheme" object: nil];
-(Void) activeLocation :( NSNotification *) y {[myLocService stopUserLocationService]; // initialize BMKLocationService myLocService = [[BMKLocationService alloc] init]; myLocService. delegate = self; // start LocationService [myLocService startUserLocationService];}
Of course, the method above may be too scum and the code is also messy.
It just provides a solution.