1、首先在plist表里边添加Privacy - Location Usage Description和NSLocationWhenInUseUsageDescription(这个是在程序使用期间进行定位,如果需要一直获取,添加NSLocationAlwaysUsageDescription),都为String类型
2、其次在.m中引入CoreLocation/CoreLocation.h头文件,并且遵循CLLocationManagerDelegate代理。然后定义一个CLLocationManager对象,代码如下:

3、开始定位的时候,首先判断是否能定位

4、代理方法

5、定位失败,调用此方法

Last but not least:
如果打印错误信息为:Error Domain=kCLErrorDomain Code=0 "(null)"。进行如下操作:
Product -> Scheme -> Edit Scheme -> Options -> 把“ Allow Location Simulation ” 选项选中,并且设置下方的“default location”就可以了,不要让这个选项为none
网友评论