【问题标题】:iOS 8 CLLocationManageriOS 8 CLLocationManager
【发布时间】:2014-12-14 16:13:31
【问题描述】:

我试图在我的控制台中只使用NSLog 坐标,但它不起作用。

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

@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);
    }
}

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

【问题讨论】:

标签: ios iphone core-location


【解决方案1】:

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

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

[self.locationManager requestWhenInUseAuthorization];

或/和

[self.locationManager requestAlwaysAuthorization];

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

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

然后你可以这样做:

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];
}

现在应该可以了。

宏源:iOS 8 Map Kit Obj-C Cannot Get Users Location

【讨论】:

    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多