1.
iOS7 the system will automatically ask you to authorize your application as soon as you start positioning
after iOS8 , the code must be implemented to require the user to authorize the application, adding the following two properties in Plist
Nslocationwheninusedescription, allowing GPS descriptions to be obtained at the front desk
Nslocationalwaysusagedescription, allows the description of GPS to be acquired in the background
2, Code
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface Viewcontroller () <cllocationmanagerdelegate>
@property(nonatomic,strong)Cllocationmanager*manger;
@end
@implementation Viewcontroller
-(cllocationmanager*) Manger
{
if (_manger= =Nil) {
_manger=[[cllocationmanager alloc]init];
}
return _manger;
}
-(void) viewdidload {
[Super viewdidload];
self. Manger. Delegate=self;
if ([[uidevice currentdevice]. Systemversion floatvalue]>8.0) {
[self. Manger requestalwaysauthorization]; Perform get location information when running in the background
//[self.manger requestwheninuseauthorization];// Get location information when the front desk executes
}
Else
{
[self. Manger startupdatinglocation];
}
}
call when the authorization status changes
-(void) Locationmanager: (cllocationmanager *) Manager didchangeauthorizationstatus: ( Clauthorizationstatus) Status
{
if (status = = kclauthorizationstatusnotdetermined) {
NSLog(@ " waiting for user authorization ");
}Else if (status = = Kclauthorizationstatusauthorizedalways | |
Status = = kclauthorizationstatusauthorizedwheninuse)
{
NSLog(@ " Authorized success ");
// start positioning
[self. Manger startupdatinglocation];
}
Else
{
NSLog(@ " authorization failed ");
}
}
-(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations: (nsarray *) Locations
{
// If you only need to get once , you can get the location and then stop
[self. Manger stopupdatinglocation];
cllocation *location=[locations lastobject];
NSLog(@ " longitude =%f, dimension =%f speed =%f", Location. Coordinate. Latitude, location. Coordinate. Longitude, location. Speed);
}
@end
1.CoreLocation use, get current position