//
// ViewController.m
// CoreLocation
//
// Created by apple on 15/5/22.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import "ViewController.h"
#import "CoreLocation/CoreLocation.h"
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *mgr ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 代理
self.mgr.delegate = self;
/**
* 在 info.plist里加入:
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述
*/
[self.mgr requestAlwaysAuthorization];//手动申请获取位置的权限
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
/**
* kCLAuthorizationStatusNotDetermined = 0,//等待授权
// This application is not authorized to use location services. Due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization
kCLAuthorizationStatusRestricted,
//无法使用定位服务,此状态用户无法修改
// User has explicitly denied authorization for this application, or
// location services are disabled in Settings.
kCLAuthorizationStatusDenied, //拒绝权限
// User has granted authorization to use their location at any time,
// including monitoring for regions, visits, or significant location changes.
kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),
//前台后台都可以访问位置的权限
// User has granted authorization to use their location only when your app
// is visible to them (it will be made visible to them if you continue to
// receive location updates while in the background). Authorization to use
// launch APIs has not been granted.
kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),
//只有在前台才能访问位置的权限
// This value is deprecated, but was equivalent to the new -Always value.
kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") = kCLAuthorizationStatusAuthorizedAlways
*/
if (status == kCLAuthorizationStatusNotDetermined)
{
NSLog(@"等待授权!");
}
else if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
NSLog(@"授权成功!");
[self.mgr startUpdatingLocation];
}
else
{
NSLog(@"授权失败");
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//打印位置信息
NSLog(@"%@",locations);
}
//懒加载
-(CLLocationManager *)mgr
{
if (!_mgr)
{
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
}
@end
locations的array信息的打印

在info.plist中添加如下配置

模拟器中便可以跳出提示框,点击allow,便会频繁打印定位信息,打印方法如上

原文:http://my.oschina.net/wupengnash/blog/418754