3
我的Swift应用程序要求允许通过CLLocationManager访问当前位置。如何在用户点击“不允许”时显示警告消息?如何在Swift中用户禁用位置时显示警报?
我的Swift应用程序要求允许通过CLLocationManager访问当前位置。如何在用户点击“不允许”时显示警告消息?如何在Swift中用户禁用位置时显示警报?
你想看看
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)!)
}))
上面的代码示出了警报,并允许用户直接去设置以启用位置。
非常棒的帮助,@MirekE!谢谢 :) –