iOS 位置服务 - 应用程序在允许之前检查位置

标签 ios geolocation cllocationmanager clgeocoder

只是试图确定用户的位置。这是代码:

self.locationManager = [[CLLocationManager alloc] init];
//self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];

self.geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: self.locationManager.location completionHandler:

 ^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Location" message:[NSString stringWithFormat:@"The app has determined that your country is: %@ (%@)", placemark.country, placemark.ISOcountryCode] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];


 }];

我第一次运行该应用程序时,会收到一个双重警告弹出窗口。 locationAlert 首先弹出,通知用户一个(空)国家/地区,然后默认弹出窗口说应用程序想要使用我的位置几乎同时弹出。如果我单击"is",下次运行该应用程序时,我将获得用户正确国家/地区的单个弹出窗口。

所以我的问题是,我如何才能首先获得访问用户位置的权限,然后运行此代码来查找他们的位置,而不会因为我在未经许可的情况下查询他们的位置而得到空响应?

最佳答案

反向地理定位代码正在尝试使用 locationManager 中的 location 属性,但在 CLLocationManager 委托(delegate)方法 之前它是无效的>locationManager:didUpdateToLocation:fromLocation: 被调用。

你应该有一个委托(delegate)方法 like this one处理位置更新。

关于iOS 位置服务 - 应用程序在允许之前检查位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16725743/

相关文章:

ios - Swift 从 UITableView 呈现弹出窗口运行速度非常慢

ios - 快速点击条形按钮项目时 UIFont 发生变化

ios - _XCTFailInCurrentTest 只应在测试运行时调用

iphone - 何时使用属性和分配初始化

android - onLocationChanged 从未调用过,GoogleApiClient

firefox - HTML5 : Remembering location setting in Firefox

iphone - 将 MKMapView 置于 userLocation 中心(最初)- 仅适用于 iPhone 4?

ios - 如何在ios中以终止状态获取每200米的位置

ios - 检测用户所在城市

objective-c - 未调用 CLLocationManager 委托(delegate)