Swift 中用户禁用位置时如何显示警报?

How to show alert when Location is disabled by the user in Swift?

我的 Swift 应用要求允许通过 CLLocationManager 访问当前位置。如何在用户点击 "Don't Allow" 时显示警告消息?

你想看

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) 

CLLocationManager.authorizationStatus()

第一个在用户更改授权状态时调用,第二个将允许您随时确定当前状态。

然后,显示消息,例如:

let alert = UIAlertController(title: "Your title", message: "GPS access is restricted. In order to use tracking, please enable GPS in the Settigs app under Privacy, Location Services.", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Go to Settings now", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) in
                print("")
                UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
            }))

上面的代码显示了一个警告,并允许用户直接进入设置以启用定位。