Use CoreLocation in iOS8
In iOS8, Apple has forced developers to obtain user authorization when requesting the location service. In addition, there is an indicator icon in the iOS status bar, prompting users whether the current application is using the location service. In addition, in iOS8, Apple further improved the positioning service, allowing developers to provide more transparency to users when requesting the positioning service. In addition, iOS8 also allows application developers to call the all-new "access monitoring" function, so that the application can obtain more positioning data only when the user permits it.
CoreLocation was used for positioning in ios8.
1. First, define a global variable to record the CLLocationManager object and introduceCoreLocation.frameworkUse#import
| 1 |
@property (nonatomic, strong) CLLocationManager *locationManager; |
2. initialize CLLocationManager and start locating
| 1 2 3 4 5 |
self.locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 10; [_locationManager startUpdatingLocation]; |
3. Implement the CLLocationManagerDelegate proxy method
(1) When location data is obtained, a CLLocation array is returned.
| |
-(Void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray *) locations {CLLocation * currLocation = [locations lastObject]; NSLog (@ longitude = % f latitude = % f Height = % f, currLocation. coordinrdinate. longpolling, currLocation. altitude );} |
(2) callback Method for retrieving user location data failure, which notifies the user
| |
-(Void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error {if ([error code] = kCLErrorDenied) {// Access Denied} if ([error code] = kCLErrorLocationUnknown) {// unable to obtain location information }} |
4.viewWillDisappearClose Positioning
| |
- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [_locationManager stopUpdatingLocation]; } |
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:
| |
Self. locationManager = [[CLLocationManager alloc] init]; _ locationManager. delegate = self; _ locationManager. desiredAccuracy = kCLLocationAccuracyBest; _ locationManager. distanceFilter = 10; [_ locationManager requestAlwaysAuthorization]; // Add this sentence [_ locationManager startUpdatingLocation]; |
2. Add the following configuration in the Info. plist file:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
The values of these two keys are the description of authorizing alert. The example configuration is as follows [Check Show Raw Keys/Values to add]: