ios - 想通过xcode开启定位服务

标签 ios objective-c

我是 ios 的新手,我想知道当我在下面使用它时如何通过编程启用定位服务....

enter image description here

当我使用这个标签来显示每当定位服务被禁用时和一个启用按钮(绿色)来启用定位服务。但是如何以编程方式实现这一点。我将如何知道定位服务被禁用以及按钮将如何启用它。请帮助我。

最佳答案

您可以使用 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/

相关文章:

ios - 在自己的应用程序中打开来自其他应用程序的文件

ios - 为什么Swift 3对OSX和iOS版本的NSString会有不同的对待?

ios - 处理程序远程通知背景

iphone - SDWebImage 取消下载

objective-c - 换肤 iOS 应用程序

c++ - 如何在我的 iPhone 应用程序中使用 C++ STL 容器?

ios - 我想在 ios Swift3 的 UITextField 中动态插入逗号

ios - swift 编译器总是 100% CPU

ios - iOS8中的通讯录授权

ios - Google Translation API 中的注音字符