我是 ios 的新手,我想知道当我在下面使用它时如何通过编程启用定位服务....
当我使用这个标签来显示每当定位服务被禁用时和一个启用按钮(绿色)来启用定位服务。但是如何以编程方式实现这一点。我将如何知道定位服务被禁用以及按钮将如何启用它。请帮助我。
最佳答案
您可以使用 CLLocationManager 的 locationServicesEnabled 属性来检查系统范围/整体可用性。使用您的 CLLocationManagerDelegate 的 locationManager: didFailWithError: 方法并检查 kCLErrorDenied 错误以查看用户是否拒绝了定位服务。
if([CLLocationManager locationServicesEnabled]){
NSLog(@"Location Services Enabled");
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
[[[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]show];
}
}
选择 2
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
if location service disable for your app then its give you error
Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"
更新
如果用户拒绝权限,我们无法以编程方式启用,用户可以手动启用位置服务,更多reference
关于ios - 想通过xcode开启定位服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34961163/