In IOS8, Apple has forced the developer to get the user's authorization when requesting location services, and there is an indication icon in the iOS status bar that prompts the user whether the location service is being used by the current app. In addition, in IOS8, Apple has further improved location services, allowing developers to request location services to provide users with more transparency. In addition, IOS8 allows app developers to call the new "Access monitoring" feature, allowing the app to get more location data when the user allows it.
IOS8 previously used corelocation positioning
1, first define a global variable used to record the Cllocationmanager object, the introduction of the CoreLocation.framework use#import <CoreLocation/CoreLocation.h>
1 |
@property (nonatomic, strong) CLLocationManager *locationManager;
|
2. Initialize the Cllocationmanager and start positioning
123)45 |
self.locationManager = [[CLLocationManager alloc]init];_locationManager.delegate = self;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;_locationManager.distanceFilter = 10;[_locationManager startUpdatingLocation];
|
3, the implementation of Cllocationmanagerdelegate agent method
(1) Gets the location data, returns an array of cllocation, typically using one of the
123)45 |
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *currLocation = [locations lastObject]; NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);}
|
(2) A callback method to get the user's location data failed, which notifies the user
12345 6789< Span class= "Line-number" >10 |
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror :(Nserror *) Error{ if ([error code] = = kclerrordenied) {
//access denied
} if ([error code] = = Kclerrorlocationunknown) {//Cannot get location information }
|
4, viewWillDisappear close the positioning
123)45 |
- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [_locationManager stopUpdatingLocation];}
|
Using corelocation positioning in iOS8
1, before using corelocation need to call the following function "IOS8 dedicated":
IOS8 has made some modifications to the positioning, including the method of locating authorization, Cllocationmanager adds the following two methods:
(1) Always allow access to location information
- (void)requestAlwaysAuthorization;
(2) Allow access to location data during use of the application
- (void)requestWhenInUseAuthorization;
Examples are as follows:
123456 |
self.locationManager = [[CLLocationManager alloc]init];_locationManager.delegate = self;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;_locationManager.distanceFilter = 10;[_locationManager requestAlwaysAuthorization];//添加这句[_locationManager startUpdatingLocation];
|
2. Add the following configuration in the Info.plist file:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
The value of these two keys is the description of the authorization alert, and the example is configured as follows [ 勾选Show Raw Keys/Values后进行添加 ]:
Resources
1, "Meet the Change of Ios8–corelocation"
2. "Core location of iOS development"
3, "IOS8 under the position of authorization"
Source transferred from: http://blog.devzeng.com/blog/ios8-corelocation-framework.html
Using corelocation positioning in iOS8