IOS CLLocationManager positioning, iOS 8 Note

Source: Internet
Author: User

IOS CLLocationManager positioning, iOS 8 Note

This afternoon, I started to use the built-in Location of IOS. I read a lot of tutorials on the Internet and run the sample code directly, but I still cannot get the location. The Code is as follows:

First ImportCoreLocation. frameworkAnd then introduce the header file.# Import

Define attributes

@property (nonatomic , strong)CLLocationManager *locationManager;
Then use the proxy CLLocationManagerDelegate

 

 

-(Void) locate {// determine whether the positioning operation is permitted if ([CLLocationManager locationServicesEnabled]) {// locate initialization _ locationManager = [[CLLocationManager alloc] init]; _ locationManager. delegate = self; _ locationManager. desiredAccuracy = kCLLocationAccuracyBest; _ locationManager. distanceFilter = 10; [_ locationManager startUpdatingLocation]; // enable location} else {// prompt that the user cannot perform location operations. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ prompt m Essage: @ positioning failed. Please confirm to enable positioning delegate: nil cancelButtonTitle: @ cancel otherButtonTitles: @ OK, nil]; [alertView show];} // start locating [_ locationManager startUpdatingLocation];} # pragma mark-CoreLocation Delegate-(void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray *) locations {// here, locations stores the location coordinate values of Continuous updates, and obtains the last value as the latest location. If you do not want it to be updated continuously, after obtaining a value in this method, let locationManager stopUpdatingLocation CLLocation * CurrentLocation = [locations lastObject]; // obtain the current city name CLGeocoder * geocoder = [[CLGeocoder alloc] init]; // returns the address information based on the latitude and longitude. [geocoder reverseGeocodeLocation: currentLocation completionHandler: ^ (NSArray * array, NSError * error) {if (array. count> 0) {CLPlacemark * placemark = [array objectAtIndex: 0]; // NSLog (@%@, placemark. name); // specific location // obtain the city NSString * city = placemark. locality; if (! City) {// city information of the four municipalities cannot be obtained through locality, but can only be obtained through the method of obtaining the province (if the city is empty, it can be known as a municipality) city = placemark. administrativeArea;} cityName = city; NSLog (@: % @, cityName); // The system updates the data until you choose to stop updating, because we only need to obtain the longitude and latitude once, we can stop updating [manager stopUpdatingLocation] After obtaining it;} else if (error = nil & [array count] = 0) {NSLog (@ No results were returned .);} else if (error! = Nil) {NSLog (@ An error occurred =%@, error) ;}}];}

In ViewDidLoadNo data is returned and the proxy is not executed after the locate is called. I have found many tutorials on the Internet. Most of the Code is similar, but the results are not satisfactory, by chance, I can see that the location of ios8 is different from that of the lower version. my mobile phone is just ios8, so I can click the blog to see it, Click here to enter the blog

 

The following is the blog summary:

In versions earlier than iOS8, CLLocationManager can be used to locate the problem, but it cannot be located in the iOS8 system recently .... This is a big problem!

Use CoreLocation in iOS8

1. Before using CoreLocation, you need to call the following function [For iOS8 ]:
IOS8 made some modifications to the positioning, including the positioning authorization method. CLLocationManager added the following two methods:
(1) Always allow access to location information

- (void)requestAlwaysAuthorization;
(2) allow access to location data during application use

 

 

- (void)requestWhenInUseAuthorization;
Example:
LocationManager = [[CLLocationManager alloc] init]; locationManager. delegate = self; locationManager. desiredAccuracy = kCLLocationAccuracyBest; locationManager. distanceFilter = 10; if (iOSVersion> = 8) {[locationManager requestWhenInUseAuthorization]; // allow access to location data during use of the program (required for iOS8 positioning)} [locationManager startUpdatingLocation]; // enable Positioning
2. Add the following configuration in the Info. plist file:
(1) NSLocationAlwaysUsageDescription
(2) NSLocationWhenInUseUsageDescription

 

Then, the positioning function will be ready for use!

 

 

 

Related Article

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.