[英]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.