IOS8 location-based message notification for new features Uilocalnotification

Source: Internet
Author: User


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;



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.