locationServicesEnabled class方法只測試位置服務的全局設置。 AFAIK,沒有辦法測試你的應用是否被明確拒絕。您必須等待位置請求失敗並使用CLLocationManagerDelegate方法locationManager:didFailWithError:來執行您所需的任何操作。例如: -
- (void)locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error {
NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"Access to Location Services denied by user";
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
errorString = @"Location data unavailable";
//Do something else...
break;
default:
errorString = @"An unknown error has occurred";
break;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
看到CLLocationManager class reference上CLError常量的文檔,更多的選擇。
來源
2010-08-10 16:36:40
Dan
這是有道理的,爲什麼它總是返回true。謝謝! – Adam 2010-08-11 00:40:04
@ Dan,如果locationServicesEnabled爲false,那麼我已經等了很長時間,但是不調用委託(locationManager:didFailWithError :)方法。是什麼原因?? – Apple 2012-06-28 06:17:49