IOS Background Continuous positioning detailed (support ISO9.0 above)
#import <CoreLocation/CoreLocation.h> and implement Cllocationmanagerdelegate agent, the complete code of. h file is as follows:
[OBJC]View Plain Copy
- #import <UIKit/UIKit.h>
- #import <CoreLocation/CoreLocation.h>
- @interface viewcontroller:uiviewcontroller<cllocationmanagerdelegate>
- @end
2.info.list file:
Right-click Add Row, the added key is nslocationalwaysusagedescription, the other values are default, examples are as follows:
3. Add Background Location permissions
4.VIEWCONTROLLER.M file:
(1) Define a private Cllocationmanager object
(2) Initialize and set the parameters (Initlocation method), where
Locationmanager.desiredaccuracy set positioning accuracy, with six values selectable, decreasing precision in turn
Kcllocationaccuracybestfornavigation
Kcllocationaccuracybest
Kcllocationaccuracynearesttenmeters
Kcllocationaccuracyhundredmeters
Kcllocationaccuracykilometer
Kcllocationaccuracythreekilometers
Locationmanager.pauseslocationupdatesautomatically set whether to allow the system to automatically pause the positioning, here to set to No, just start I did not set, background location for about 20 minutes to stop!
(3) Implement the Proxy method for Cllocationmanagerdelegate, which is called after each successful location:
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray*) locations;
* You can also do this by implementing the following methods:
-(void) Locationmanager: (Cllocationmanager *) Manager
Didupdatetolocation: (cllocation *) newlocation
Fromlocation: (cllocation *) oldlocation
(4) Implement the Proxy method of Cllocationmanagerdelegate, which is called after locating an error:
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error
The complete code for the. m file is as follows:[OBJC]View Plain Copy
- #import "ViewController.h"
- @interface Viewcontroller () {
- Cllocationmanager *locationmanager;
- cllocation *newlocation;
- Cllocationcoordinate2D coordinate;
- }
- @end
- @implementation Viewcontroller
- -(void) Viewdidload {
- [Super Viewdidload];
- [self initlocation];
- }
- #pragma mark Initialize the location
- -(void) initlocation {
- Locationmanager=[[cllocationmanager alloc] init];
- Locationmanager. Delegate = self ;
- Locationmanager. desiredaccuracy = kcllocationaccuracybest; Set positioning accuracy
- if ([[[[ Uidevice Currentdevice] systemversion] Floatvalue] >= 8. 0) {
- [Locationmanager requestalwaysauthorization];
- }
-
9.0 After this must be added is not able to achieve the background of continuous positioning
if ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 9.0) {
Locationmanager.allowsbackgroundlocationupdates = YES;
}
- if (![ Cllocationmanager locationservicesenabled]) {
- NSLog (@ "Please turn on location: Settings > Privacy > Locations > Location Services");
- }
- Locationmanager. pauseslocationupdatesautomatically = NO;
- [Locationmanager startupdatinglocation];
- //[locationmanager Startmonitoringsignificantlocationchanges];
- }
- #pragma Mark successfully positioned
- -(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations: (nsarray *) locations{
- NewLocation = [Locations Lastobject];
- double lat = newlocation. Coordinate. latitude;
- Double lon = newlocation. Coordinate. Longitude;
- NSLog (@ "lat:%f,lon:%f", Lat,lon);
- }
- #pragma mark failed to locate
- -(void) Locationmanager: (cllocationmanager *) Manager Didfailwitherror: (nserror *) error{
- NSLog (@ "error:%@", error);
- }
- -(void) didreceivememorywarning {
- [Super didreceivememorywarning];
- //Dispose of any resources, can be recreated.
- }
- @end
IOS Background Continuous positioning detailed (support ISO9.0 above)