【问题标题】:How to show alert when Location is disabled by the user in Swift?当用户在 Swift 中禁用位置时如何显示警报?
【发布时间】:2015-11-07 04:46:03
【问题描述】:
我的 Swift 应用程序要求允许通过 CLLocationManager 访问当前位置。当用户点击“不允许”时如何显示警告消息?
【问题讨论】:
标签:
swift
ios8
xcode6
core-location
cllocationmanager
【解决方案1】:
你想看看
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)!)
}))
上面的代码显示了一个警报,并允许用户直接进入设置以启用位置。