IOS development and ios development tutorial

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.