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!謝謝 :) –