使用Swift,UIAlertController提示用户可以打开位置设置吗?

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)

但是这个功能打开设置应用程序,而不是位置设置.我需要打开(设置 - >隐私 - >位置)

谢谢

dim*_*iax 9

从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)


viv*_*ani 7

首先在项目中配置URL Schemes.您可以在Target - > Info - > URL Scheme中找到它.单击+按钮并在URL Schemes中键入prefs

在此输入图像描述

现在替换if let url = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = NSURL(string: "prefs:root=LOCATION_SERVICES"))

以下是所有可用的网址

  • 首选项:根=通用及路径=关于
  • 首选项:根=通用及路径= ACCESSIBILITY
  • 首选项:根= AIRPLANE_MODE
  • 首选项:根=通用及路径= AUTOLOCK
  • 首选项:根=通用及路径= USAGE/CELLULAR_USAGE
  • 首选项:根=亮度
  • 首选项:根=通用及路径=蓝牙
  • 首选项:根=通用及路径= DATE_AND_TIME
  • 首选项:根= FACETIME
  • 首选项:根=通用
  • 首选项:根=通用及路径=键盘
  • 首选项:根= CASTLE
  • 首选项:根=城堡&路径= STORAGE_AND_BACKUP
  • 首选项:根=通用及路径= INTERNATIONAL
  • 首选项:根= LOCATION_SERVICES
  • 首选项:根= ACCOUNT_SETTINGS
  • 首选项:根= MUSIC
  • 首选项:根=音乐与路径= EQ
  • 首选项:根=音乐与路径= VolumeLimit
  • 首选项:根=通用及路径=网络
  • 首选项:根= NIKE_PLUS_IPOD
  • 首选项:根= NOTES
  • 首选项:根= NOTIFICATIONS_ID
  • 首选项:根=电话
  • 首选项:根=照片
  • 首选项:根=通用及路径= ManagedConfigurationList
  • 首选项:根=通用及路径=复位
  • 首选项:根=声音和路径=铃声
  • 首选项:根= Safari浏览器
  • 首选项:根=通用及路径=助理
  • 首选项:根=声音
  • 首选项:根=通用及路径= SOFTWARE_UPDATE_LINK
  • 首选项:根= STORE
  • 首选项:根= TWITTER
  • 首选项:根= FACEBOOK
  • prefs:root = General&path = USAGE prefs:root = VIDEO
  • 首选项:根=通用及路径=网络/ VPN
  • 首选项:根=壁纸
  • 首选项:根= WIFI
  • 首选项:根= INTERNET_TETHERING