iOS development notebook-IOS8 using location Services Solutions

Source: Internet
Author: User

Problem Description:

Before IOS8, when the app first starts positioning the service, a prompt pops up to let the user choose whether to allow location information. However, after IOS8, the app will not appear this pop-up window. After the first run, your app doesn't have any settings, not "never" or "Always," in Settings---privacy----location services.

The code is as follows:

#import "XYZFirstViewController.h"@interfaceXyzfirstviewcontroller ()-(Ibaction) Locatebuttonclick: (ID) sender;@end@implementationXyzfirstviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (startlocate) Name:@"startlocatenotification" Object: nil]; _locationmanager=[[Cllocationmanager alloc] init]; _locationmanager.Delegate=Self ; _locationmanager.desiredaccuracy=kcllocationaccuracybest; _locationmanager.distancefilter=1000.0f; _mapview.maptype=Mkmaptypestandard; _mapview.Delegate=Self ; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated]; [_locationmanager startupdatinglocation];}-(void) Viewwilldisappear: (BOOL) animated{[Super viewwilldisappear:animated]; [_locationmanager stopupdatinglocation];}-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation*currentlocation=[Locations Lastobject]; _currentlocation=currentlocation; Self.currentLocationLabel.text=[nsstring stringWithFormat:@"%3.5f,%3.5f,%3.5f", Currentlocation.coordinate.longitude,currentlocation.coordinate.latitude,currentlocation.altitude]; Mkcoordinateregion Region=mkcoordinateregionmakewithdistance (Currentlocation.coordinate, +, +);    [_mapview setregion:region Animated:yes]; Mkpointannotation*point=[[Mkpointannotation alloc] init]; Point.coordinate=_currentlocation.coordinate; Point.title=@"My location"; [_mapview addannotation:point];}-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{NSLog (@"error:%@", error);}/*#pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before n avigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [s]    Egue Destinationviewcontroller]. Pass the selected object to the new view controller.}*/-(Ibaction) Locatebuttonclick: (IDsender {[[Nsnotificationcenter defaultcenter] Postnotificationname:@"startlocatenotification" Object: self];}-(void) startlocate{Clgeocoder*geocoder=[[Clgeocoder alloc]init]; [Geocoder reversegeocodelocation:_currentlocation Completionhandler:^ (Nsarray *placemarks, Nserror *error) {        if([Placemarks count]>0) {NSLog (@"%@", Placemarks); Clplacemark*placemark=placemarks[0]; Nsdictionary*addressdictonary=placemark.addressdictionary; _currentaddresslabel.text=[nsstring stringWithFormat:@"%@,%@,%@", [Addressdictonary Objectforkey: (NSString *) kabpersonaddressstatekey],[addressdictonary ObjectForKey: (NSString *) Kabpersonaddresscitykey],[addressdictonary Objectforkey: (NSString *) [Kabpersonaddressstreetkey]];            }     }]; }@end

Solution:

The above code needs to manually invoke the requestalwaysauthorization/of the Cllocationmanager object after iOS8

Requestwheninuseauthorization method. Calling this method requires setting the value of Nslocationalwaysusagedescription/nslocationwheninuseusagedescription in Info.plist, which is displayed in the System prompt box.

The code is as follows:

-(void) viewwillappear: (BOOL) animated{    [Super viewwillappear:animated];    [_locationmanager requestwheninuseauthorization];    [_locationmanager startupdatinglocation];}

The Info.plist settings are as follows:

Allowed Effects:

iOS development notebook-IOS8 using location Services Solutions

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.