iOS background targeting

Source: Internet
Author: User

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

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.