Giu*_*ppe 3 swift xcode6 uialertcontroller
在我的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)
}
Run Code Online (Sandbox Code Playgroud)
但是这个功能打开设置应用程序,而不是位置设置.我需要打开(设置 - >隐私 - >位置)
谢谢
从iOS8,您可以从自定义应用程序打开设置系统位置.
斯威夫特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)
Run Code Online (Sandbox Code Playgroud)
斯威夫特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)
Run Code Online (Sandbox Code Playgroud)
首先在项目中配置URL Schemes.您可以在Target - > Info - > URL Scheme中找到它.单击+按钮并在URL Schemes中键入prefs
现在替换if let url = NSURL(string: UIApplicationOpenSettingsURLString)
为 if let url = NSURL(string: "prefs:root=LOCATION_SERVICES"))
以下是所有可用的网址
| 归档时间: |
|
| 查看次数: |
2495 次 |
| 最近记录: |