ios - 尝试在 iOS Swift 中获取位置权限

标签 ios swift permissions core-location

我正在开发一个按钮,如果他授予应用程序位置权限,该按钮会将用户发送到 map 应用程序。问题是,如果我单击该按钮一次并拒绝位置许可,则下次我单击该按钮时,该应用程序不会再次请求许可。似乎用户“只有一次机会授予 map 权限”。

下面是我用来以编程方式创建带有回调函数的按钮的代码。还有一个“自定义 iOS 目标属性”的屏幕截图,其中我包含了隐私 - 位置使用说明以便能够使用用户位置。

directionsButton.addTarget(self, action: #selector(getDirection), for:   UIControlEvents.touchUpInside)


func getDirection(sender: UIButton!) {
    print("1")
    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
       let url = URL(string: "http://maps.apple.com/?daddr=60.79281049999999,10.688968899999963")
       if (UIApplication.shared.canOpenURL(url!)) {
            UIApplication.shared.openURL(url!)
        } else {
            print("Error")
        }
        print("2")
    } else{
        LocationManager.requestWhenInUseAuthorization()
        print("3")
    }
    print("4")
}

/*
 - Callback function for changes in location permissions
 */
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus){
    print("change in auth")
    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){
        let url = URL(string: "http://maps.apple.com/?daddr=60.79281049999999,10.688968899999963")
        if (UIApplication.shared.canOpenURL(url!)) {
            UIApplication.shared.openURL(url!)
        } else {
            print("Error")
        }
    } else{
        self.view.makeToast("Couldn't get location permission", duration: 3.0, position: .bottom)
    }
}

Picture of permissions added in the info-file

最佳答案

Seems like the user have "only one shot at granting maps permission".

是的,位置权限就是这种情况,地址簿访问权限等其他事情也是如此。 您的应用程序可以检测到用户之前拒绝了许可,如果是,则告诉他们他们需要通过设置启用它,并使用一个名为“带我去设置”或其他名称的按钮。 单击按钮后,您可以通过以下方式启动应用程序设置:

 UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: { (results) in
    ...
     })

关于ios - 尝试在 iOS Swift 中获取位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42164357/

相关文章:

ios - 获取星期几

ios - 如何使用 XCode 5 在 IB 中设置 View 的大小?

arrays - 如何在 xcode macos 中显示图像数组或网格中的任何对象数组

php - 如何允许 www-data apache 用户使用 mkdir() php 函数?

linux - vsftpd 文件夹上不显示任何内容

ios - facebook ios api问题

ios - 外观不佳的 UILabel - 缺少抗锯齿功能?

swift - ld : truncated fat file for manual added framework

swift - 无法将类型 '[NSLayoutConstraint]' 的值转换为预期的参数类型 'NSLayoutConstraint'

android - Android 权限以什么顺序显示?