简体   繁体   English

尝试在iOS Swift中获取位置许可

[英]Trying to get location permission in iOS Swift

I'm working on a button that will send the user to the maps application if he grants the app location permission. 我正在使用一个按钮,如果他授予应用程序位置权限,该按钮会将用户发送到地图应用程序。 The problem is that if I click the button once and decline location permission, the next time I'm clicking the button the app doesn't ask for permission again. 问题是,如果我单击一次按钮并拒绝位置许可,那么下次我单击该按钮时,应用程序不会再次请求许可。 Seems like the user have "only one shot at granting maps permission". 好像用户“在授予地图权限时只有一张”。

Below is the code I use to programmatically create the button with the callback functions. 下面是我用于通过回调函数以编程方式创建按钮的代码。 There's also a screenshot of the "Custom iOS Target Properties" where I have included the Privacy - Location Usage Description to be able to use users location. 还有一个“自定义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". 好像用户“在授予地图权限时只有一张”。

Yes that is the case with the location permission, as it is for other things such as address book access permission. 是的,具有位置许可的情况就是如此,其他情况(例如通讯簿访问许可)也是如此。 Your app could detect that the user has refused permission previously, and if so tell them they need to enable it via settings with a button called something like "Take me to settings" or whatever. 您的应用可能会检测到用户之前曾拒绝过权限,如果这样,则告诉他们他们需要通过带有“将我带入设置”之类的按钮的设置来启用它。 On button click you can launch the settings for the app via this: 在按钮上单击,您可以通过以下方式启动应用程序的设置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM