gpt4 book ai didi

ios - CLLocation 当前位置不工作

转载 作者:行者123 更新时间:2023-11-28 19:35:32 24 4
gpt4 key购买 nike

我的类 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];
}
}

但我没有获取允许访问的位置或弹出窗口。

我正在使用 Core Location 框架。

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

我正在模拟器上测试这个。

最佳答案

有时模拟器无法与启用位置的服务一起使用 Apple Device 进行完美测试。

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

enter image description here

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

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

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

关于ios - CLLocation 当前位置不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com