gpt4 book ai didi

ios - 在 iOS 8 中获取 CoreLocation

转载 作者:行者123 更新时间:2023-11-28 21:46:40 25 4
gpt4 key购买 nike

我正在尝试通过我的应用获取当前用户位置。我编写了所有委托(delegate)方法,在 info.plist 中添加了字符串,但它仍然没有调用委托(delegate)方法。请帮助我。

 - (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined &&([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
// choose one request according to your business.
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
}
[self.locationManager startUpdatingLocation];

}

#pragma mark - CLLocationManagerDelegate
- (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) {
longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}

Screenshot of added keys in my app info.plist Error finding keys

最佳答案

控制台日志本身说您缺少在 info.plist 文件中添加适当的 key 你应该添加

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

带有相关描述的 key ,然后请求用户允许访问。

link可能会有帮助。

因为您已经添加了这些 key ,请尝试以下操作

  1. 删除 key 并重新添加。
  2. 完成后尝试清理然后构建您的应用程序。

关于ios - 在 iOS 8 中获取 CoreLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901101/

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