Use CoreLocation in iOS8

Source: Internet
Author: User

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]:


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.