iOS geolocation app normal operation can, press the home button after the app in the background can also, double-click the Home button to kill the app can also, even after restarting the machine can also. (IOS 10 test code)
1) Set some request parameters
just like normal. Cllocationmanager request permission as well as background update requests
Background update:
Plist request geographic location (always used to run)
2) Follow the same steps as usual to initialize the location manager
Cllocationmanager *locationmgr = [[Cllocationmanager alloc]init];
[Locationmgr Setallowsbackgroundlocationupdates:yes];
Determine if the phone has location services enabled
if (![ Cllocationmanager locationservicesenabled]) {
NSLog (@ "Location service may not be open now, please set open! ");
Return
}
else {
NSLog (@ "Location service is turned on");
}
//Request user authorization if not authorized
if ([Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined) {
[Locationmgr requestalwaysauthorization];
NSLog (@ "Request user authorization");
}else
{
NSLog (@ "can be used");
Locationmgr.delegate = self;
Locationmgr.desiredaccuracy = kcllocationaccuracybestfornavigation;
Locationmgr.distancefilter = 0.1;
Locationmgr.allowsbackgroundlocationupdates = YES;
[Locationmgr startupdatinglocation];
[Locationmgr startmonitoringsignificantlocationchanges];
}
If the location is changed, go downstairs. Callback function
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) locations{}
3) function to set the background and destroy state
Call at end
[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (hehe) name:@ "OK" object: Uiapplicationwillterminatenotification];
Called when background
[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (hehe) name:@ "OK" object: Uiapplicationdidenterbackgroundnotification];
-(void) hehe{
[Locationmgr Startmonitoringsignificantlocationchanges];
[Locationmgr startupdatinglocation];
}
-------------> Everything
Attention:
1. The app is killed and the callback function is slow when you start
2. Here are the Startmonitoringsignificantlocationchanges and startupdatinglocation two methods, one is a significant position change one is normal, the former is killed can also evoke go into the callback function, The latter must be invoked to go into the callback function when the app is not dead.
Report:
Startmonitoringsignificantlocationchanges:
Https://developer.apple.com/reference/corelocation/cllocationmanager/1423531-startmonitoringsignificantlocati
( meaning that even if killed, it can be evoked by a significant change in position )
Startupdatinglocation:
Https://developer.apple.com/reference/corelocation/cllocationmanager/1423750-startupdatinglocation
( meaning that the app will not be awakened by a change in position )
-=> can do this by developing apps that need to be launched by the app itself! Laugh
iOS background targeting