IOS Development notes-use the positioning service solution in ios8.

Source: Internet
Author: User

IOS Development notes-use the positioning service solution in ios8.
Problem description: Before iOS8, when the app first starts to locate the service, a prompt box will pop up asking the user to choose whether to allow location information. However, after ios8. After the first operation, in Settings> privacy> positioning service, your app has no settings, neither "never" nor "always ". The Code is as follows: copy the Code # import "XYZFirstViewController. h "@ interface XYZFirstViewController ()-(IBAction) LocateButtonClick :( id) sender; @ end @ implementation XYZFirstViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (startLocate) name: @ "startLocateNotification" object: nil]; _ locatio NManager = [[CLLocationManager alloc] init]; _ locationManager. delegate = self; _ locationManager. desiredAccuracy = kCLLocationAccuracyBest; _ locationManager. distanceFilter = 1000.0f; _ mapView. mapType = MKMapTypeStandard; _ mapView. delegate = self;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(void) viewWillAppear :( BOOL) animated {[super ViewWillAppear: animated]; [_ locationManager startUpdatingLocation];}-(void) viewWillDisappear :( BOOL) animated {[super ready: animated]; [_ locationManager stopUpdatingLocation) 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. longpolling, currentLocation. coordinate. latitude, currentLocation. altitude]; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (currentLocation. coordinate, 1000,100 0); [_ mapView setRegion: region animated: YES]; MKPointAnnotation * point = [[MKPointAnnotation alloc] init]; point. coordinate = _ currentLocation. coordinate; p Oint. 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 will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view cont Roroller using [segue destinationViewController]. // Pass the selected object to the new view controller .} */-(IBAction) LocateButtonClick :( id) sender {[[nsicationicationcenter defacenter center] postNotificationName: @ "startLocateNotification" object: self];}-(void) startLocate {CLGeocoder * geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation: _ currentLocation completionHandler: ^ (NSArray * p LaceMarks, 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 *) Comment ], ObjectForKey :( NSString *) kABPersonAddressStreetKey] ;}}] ;}@ end copy code solution: After iOS8, you must manually call the requestAlwaysAuthorization/requestWhenInUseAuthorization method of the CLLocationManager object. To call this method, you must set the value of NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription in Info. plist. The value is displayed in the system prompt box. The Code is as follows:-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; [_ locationManager requestWhenInUseAuthorization]; [_ locationManager startUpdatingLocation];}

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.