首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 8 CLLocationManager

iOS 8 CLLocationManager
EN

Stack Overflow用户
提问于 2014-10-20 00:38:16
回答 1查看 186关注 0票数 0

我正尝试在我的控制台上执行NSLog坐标,但它不起作用。

我在标题中链接了核心位置

代码语言:javascript
复制
@implementation ViewController {
    CLLocationManager *locationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        NSLog(@"%f", currentLocation.coordinate.longitude);
        NSLog(@"%f", currentLocation.coordinate.latitude);
    }
}

但是我在控制台上没有得到任何东西,有人知道可能会出什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2014-10-20 01:14:02

从iOS 8开始,在开始更新位置之前,你必须征得用户的许可。在此之前,您必须添加用户将在权限请求中收到的消息。在您的.plist文件中添加这两个键(如果您想使用两种类型的位置获取),并用您自己的消息填充它们:NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

然后请求许可,就在启动CLLocationManager之前

代码语言:javascript
复制
[self.locationManager requestWhenInUseAuthorization];

或/和

代码语言:javascript
复制
[self.locationManager requestAlwaysAuthorization];

为了避免在iOS 7及更低版本上崩溃,您可以定义一个宏来检查操作系统版本:

代码语言:javascript
复制
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

然后你可以这样做:

代码语言:javascript
复制
if(IS_OS_8_OR_LATER) {
        // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
}

现在它应该可以工作了。

宏源:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26452785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档