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

时间:2013-05-23 23:51:44

标签: 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首先弹出,通知用户一个(null)国家,然后默认弹出窗口显示应用程序想要使用我的位置几乎同时弹出。如果我单击“是”,则下次运行应用程序时,我将获得用户正确国家/地区的单个弹出窗口。

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

1 个答案:

答案 0 :(得分:2)

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

您应该有一个委托方法like this one来处理位置更新。