IOS8 previously used with iOS8 corelocation positioning

Source: Internet
Author: User

IOS8 previously used corelocation positioning
1, first define a global variable to record Cllocationmanager object, introduce corelocation.framework use #import <CoreLocation/CoreLocation.h>
1

@property (nonatomic, strong) Cllocationmanager *locationmanager;

2. Initialize the Cllocationmanager and start positioning
-(Cllocationmanager *) Locationmanager
{
#warning location service is not available, directly returns null
if (![ Cllocationmanager locationservicesenabled]) return nil;

if (!_locationmanager) {
Create locator
Self.locationmanager = [[Cllocationmanager alloc]init];
Set up Proxy
Self.locationManager.delegate = self;
}
return _locationmanager;
}

-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];

Self.locationManager.desiredAccuracy = Kcllocationaccuracybest;
Self.locationManager.distanceFilter = 10;
[Self.locationmanager startupdatinglocation];

}


3, the implementation of Cllocationmanagerdelegate agent method
(1) Gets the location data, returns an array of cllocation, typically using one of the

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
Cllocation *currlocation = [locations Lastobject];
NSLog (@ "Longitude =%f latitude =%f height =%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

-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
if ([error code] = = kclerrordenied)
{
Access is denied
}
if ([error code] = = Kclerrorlocationunknown) {
Unable to get location information
}
}

4. Close the position in Viewwilldisappear

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


-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];


Self.locMgr.desiredAccuracy = Kcllocationaccuracybest;
Self.locMgr.distanceFilter = 10;
#warning Location Services, iOS8 add this sentence later
[Self.locmgr requestalwaysauthorization];

[Self.locmgr startupdatinglocation];

}

2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription

IOS8 previously used with iOS8 corelocation positioning

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.