CLLocation当前位置无效 [英] CLLocation current location not working

查看:112
本文介绍了CLLocation当前位置无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级 ViewController 中有此代码:

CLLocationManager *locationManager;

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

    locationManager = [[CLLocationManager alloc] init];
    [locationManager requestWhenInUseAuthorization];
}


- (IBAction)getCurrentLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Did finish with error - %@", error);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSLog(@"did Update Location - %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if(currentLocation != nil) {
        _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}

但我没有得到该位置或弹出窗口允许访问。

But I am not getting the location or the popup for allowing access.

我正在使用核心位置框架。

I am using the Core Location framework.

在按钮上单击我正在打印纬度,经度和标签中的地址。

On button click I am printing latitude, longitude and address in labels.

我正在模拟器上对此进行测试。

I am testing this on the simulator.

推荐答案

有时Simulator无法与启用了位置的服务一起使用,请使用 Apple Device 进行完美的测试。

Sometimes Simulator wont work with location enabled services use Apple Device for perfect testing.

添加以下密钥在您的 info.plist 文件中以获取允许访问弹出窗口。

Add the following keys inside your info.plist file to get allow access popup.

或将其添加为更新 info.plist 文件源代码。

or add them as updating info.plist file source code.

<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>

这篇关于CLLocation当前位置无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆