Apple has officially released its new iOS8 operating system on WWDC2014. The iOS8 on the interface is not much different from iOS7, but it is perfect in function.
IOS8 updated and exposed a lot of interfaces, including a local message to notify Uilocalnotification, we certainly are not unfamiliar. However, it is optimized and improved in iOS8. Now it can initiate a message notification based on geographic location, that is, we set a coordinate (latitude and longitude) and radius (range) in the app, and when the device with the app enters the area, the app sends a message notification.
Here's how:
1. To import the class library we need Corelocation.framework
2. Register location information to obtain the user's authorization
Cllocationmanager *locman = [[Cllocationmanager alloc] init];locman.delegate = self;//Request Authorization to track the User ' s Location[locman requestwheninuseauthorization];
also configure the plist file
when you run to the last sentence, the user is prompted to allow the app to be authorized.
3. The app will be callback after obtaining the authorization
-(void) Locationmanager: (Cllocationmanager *) Manager Didchangeauthorizationstatus: (clauthorizationstatus) Status { //check status to see if we ' re authorized BOOL canuselocationnotifications = (Status = = Kclauthoriza Tionstatusauthorizedwheninuse); if (canuselocationnotifications) { [self startshowinglocationnotifications]; }}
callback method Registration notice
-(void) startshowingnotifications {uilocalnotification *locnotification = [[Uilocalnotification alloc] INIT]; Locnotification.alertbody = @ "you have arrived!"; Locnotification.regiontriggersonce = YES; Locnotification.region = [[Clcircularregion alloc] Initwithcenter:loc_coordinate Radius:loc_radius Identifier:loc_identifier]; [[UIApplication sharedapplication] schedulelocalnotification:localnotification];} -(Instancetype) Initwithcenter: (cllocationcoordinate2d) Center//region central latitude and longitude// radius: (cllocationdistance) Radius//area radius range// IdentiFiER: (nsstring *) identifier; Description of the notification's unique designation
4. Triggered message notification after a certain area, the app callback method after receiving the message
-(void) Locationmanager: (Cllocationmanager *) Manager didenterregion: (clregion *) region{NSLog (@ "%s", __func__);} -(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{ clregion *region = notification.region; if (region) { [self tellfriendsuserarrivedatregion:region]; }}
Note:
Check out the official documentation to learn more about the Uilocalnotification new API;
The service needs location information registration;
If the location information is disabled, this method application:didreceivelocalnotification: it will not be called;