技术文章分类(180)

技术随笔(11)

IOS8定位

注意一个事情,因为定位可能是个全局可用的方法,所以我经常提出来,这样可以减少工作量,可是ios8以后,我提取出去就不能弹窗让用户授权定位了。

所以授权定位,最好写在ViewController里,如果有人能够有更好的办法,求告知841538513@qq.com,十分感谢

1,在info.plist设置NSLocationAlwaysUsageDescription,NSLocationWhenInUseUsageDescription为你想要标志的string描述

  • NSLocationWhenInUseUsageDescription表示应用在前台的时候可以搜到更新的位置信息。
  • NSLocationAlwaysUsageDescription表示应用在前台和后台(suspend或terminated)都可以获取到更新的位置数据。

2,导入CoreLocation.framework

3,导入代码

这里注意:CLLocationManager变量一定要设置成全局的,至于为什么我也不知道,虽然不合理,但是必须要这么做。

而且CLLocationManager变量一定要和CLLocationManagerDelegate实现方法在一个controller里,虽然很奇怪,但就是这样。

我知道ios7以前是可以不这样的,我有提炼出来,估计这是ios8隐藏的bug吧。

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager  *locationManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = 10;
    [_locationManager requestAlwaysAuthorization];
    [_locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currLocation = [locations lastObject];
    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
}

@end

注意第一点,和一些细节。

posted @ 2014-12-06 21:41  坤哥MartinLi  阅读(238)  评论(0编辑  收藏  举报