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

时间:2015-04-01 18:57:14

标签: 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)
}

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

由于

3 个答案:

答案 0 :(得分:9)

从iOS8

,您可以从自定义应用程序打开设置系统位置。

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

enter image description here

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

以下是所有可用的网址

  • prefs:root = General& path = About
  • 首选项:根=通用及安培;路径= ACCESSIBILITY
  • prefs:root = AIRPLANE_MODE
  • 首选项:根=通用及安培;路径= AUTOLOCK
  • prefs:root = General& path = USAGE / CELLULAR_USAGE
  • 首选项:根=亮度
  • 首选项:根=通用及安培;路径=蓝牙
  • prefs:root = General& path = DATE_AND_TIME
  • 首选项:根= FACETIME
  • prefs:root = General
  • prefs:root = General& path = Keyboard
  • 首选项:根= CASTLE
  • 首选项:根=城堡&安培;路径= STORAGE_AND_BACKUP
  • prefs:root = General& path = INTERNATIONAL
  • 首选项:根= LOCATION_SERVICES
  • prefs:root = ACCOUNT_SETTINGS
  • prefs:root = MUSIC
  • 首选项:根=音乐与安培;路径= EQ
  • prefs:root = MUSIC& path = VolumeLimit
  • 首选项:根=通用及安培;路径=网络
  • prefs:root = NIKE_PLUS_IPOD
  • 首选项:根= NOTES
  • prefs:root = NOTIFICATIONS_ID
  • prefs:root = Phone
  • 首选项:根=照片
  • 首选项:根=通用及安培;路径= ManagedConfigurationList
  • prefs:root = General& path = Reset
  • 首选项:根=声音和安培;路径=铃声
  • prefs:root = Safari
  • prefs:root = General& path = Assistant
  • 首选项:根=声音
  • prefs:root = General& path = SOFTWARE_UPDATE_LINK
  • 首选项:根= STORE
  • 首选项:根= TWITTER
  • prefs:root = FACEBOOK
  • prefs:root = General& path = USAGE prefs:root = VIDEO
  • prefs:root = General& path = Network / VPN
  • 首选项:根=壁纸
  • prefs:root = WIFI
  • 首选项:根= INTERNET_TETHERING

答案 2 :(得分:-1)

打开应用程序设置。 Xcode 8.2.1 - iOS 10

if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}