在我的Swift应用程序中,我检查 locationServiceEnabled()。如果它返回false,我应该使用UIAlertController提示用户进行位置设置。 可以这样做吗? 我使用此函数提示应用程序的位置设置:
func displayAlertToEnableLocationServicesApp()
{
let alertController = UIAlertController(
title: "Location Access is Turned Off",
message: "In order to locate your position, please open settings and set location access to 'While Using the App'",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Settings", style: .Default) { (action) in
if let url = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
但是这个功能打开设置应用程序,而不是位置设置。 我需要打开(设置 - >隐私 - >位置)
由于
答案 0 :(得分:9)
,您可以从自定义应用程序打开设置系统位置。
Swift 3
let alertVC = UIAlertController(title: "Geolocation is not enabled", message: "For using geolocation you need to enable it in Settings", preferredStyle: .actionSheet)
alertVC.addAction(UIAlertAction(title: "Open Settings", style: .default) { value in
let path = UIApplicationOpenSettingsURLString
if let settingsURL = URL(string: path), UIApplication.shared.canOpenURL(settingsURL) {
UIApplication.shared.openURL(settingsURL)
}
})
alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
viewController.present(alertVC, animated: true, completion: nil)
Swift 2.3
let alertVC = UIAlertController(title: "Geolocation is not enabled", message: "For using geolocation you need to enable it in Settings", preferredStyle: .ActionSheet)
alertVC.addAction(UIAlertAction(title: "Open Settings", style: .Default) { value in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
})
alertVC.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
viewController.presentViewController(alertVC, animated: true, completion: nil)
答案 1 :(得分:7)
首先在项目中配置URL Schemes。你会在Target中找到它 - >信息 - >网址方案。单击+按钮并在URL方案中键入prefs
现在替换if let url = NSURL(string: UIApplicationOpenSettingsURLString)
与if let url = NSURL(string: "prefs:root=LOCATION_SERVICES"))
以下是所有可用的网址
答案 2 :(得分:-1)
打开应用程序设置。 Xcode 8.2.1 - iOS 10
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}