iOS 2个库支库:
mapkit 地图 core Location 用于地理定位
core Location框架使用的前提
-
导入coreLocation.framework
-
引入头文件 #import <coreLocation/coreLocation.h>
-
所有数据类型的前缀CL
* iOS8需要配置info.plist表
#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>@property (nonatomic, strong) CLLocationManager *mgr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //1.创建CLLocationManager self.mgr = [CLLocationManager new]; //2.请求授权 从iOS8开始必须在应用中请求授权 if ([self.mgr respondsToSelector:@selector(requestWhenInUseAuthorization)]) { //当用户使用的时候授权 [self.mgr requestWhenInUseAuthorization]; } //3.设置代理 self.mgr.delegate = self; //调用开始定位的方法 [self.mgr startUpdatingLocation];
}
#pragma mark - CoreLocationDelegate -
//当完成用户位置更新的时候调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"%@",locations);
//结束持续打印
[self.mgr stopUpdatingLocation];
}
@end