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