In previous versions of IOS8, we had no problem using cllocationmanager positioning, and recently in the IOS8 system it was not possible to locate .... This is a big problem!
1, first define a global variable to record Cllocationmanager object, introduce corelocation.framework use #import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) Cllocationmanager *locationmanager;
2. Initialize the Cllocationmanager and start positioning
Locationmanager=[[cllocationmanager alloc] init]; locationmanager.delegate=self; Locationmanager.desiredaccuracy=kcllocationaccuracybest; locationmanager.distancefilter=10; [Locationmanager startupdatinglocation];//open position
3, the implementation of Cllocationmanagerdelegate agent method
#pragma Mark Cllocationmanagerdelegate
/**
* Get latitude and longitude
*/-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{ cllocation * Currlocation=[locations Lastobject]; Location.strlatitude=[nsstring stringwithformat:@ "%f", currLocation.coordinate.latitude]; Location.strlongitude=[nsstring stringwithformat:@ "%f", currLocation.coordinate.longitude]; NSLog (@ "La---%f, lo---%f", currlocation.coordinate.latitude,currlocation.coordinate.longitude);} /** * location failed, callback this method */-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{ if ([ Error code]==kclerrordenied) { NSLog (@ "Access Denied"); } if ([Error Code]==kclerrorlocationunknown) { NSLog (@ "Cannot get location information");} }
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:
Locationmanager=[[cllocationmanager alloc] init]; locationmanager.delegate=self; Locationmanager.desiredaccuracy=kcllocationaccuracybest; locationmanager.distancefilter=10; if (iosversion>=8) { [Locationmanager requestwheninuseauthorization];//allows access to location data in the course of the program (IOS8 location required) } [Locationmanager startupdatinglocation];//open position
2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription
After this is added, the positioning function will be able to use the normal!
IOS Cllocationmanager Targeting