繁体   English   中英

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

[英]iOS Location Services - App is checking for location before allowed

仅尝试确定用户的位置。 这是代码:

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

所以我的问题是,我如何才能首先获得访问用户位置的权限,然后运行此代码以查找其位置,而不会由于我未经允许便查询了他们的位置而得到空响应?

反向地理位置代码试图使用location从属性locationManager ,但它是无效的,直到CLLocationManager委托方法locationManager:didUpdateToLocation:fromLocation:被调用。

您应该有一个像这样的委托方法来处理位置更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM