swift - 使用 Swift,是否可以使用 UIAlertController 提示用户打开位置设置?

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

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

谢谢

最佳答案

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

关于swift - 使用 Swift,是否可以使用 UIAlertController 提示用户打开位置设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29398644/

相关文章:

arrays - 转换字符串?

ios - 从过滤数据崩溃的 TableView 中删除项目

ios - 如何在 Snapguide 上获得模糊的主图像颜色

objective-c - 如何使用 posix_spawn 替换已弃用的 'system' 以在 Objective-C 中启动 opendiff?

ios - 当呈现 actionSheet (AlertController) 时,如何修复 PopoverViewController 的边界?

ios8 - 在 iOS8 中的 UIActionSheet 中添加 UIPickerView 的最佳解决方案是什么?

ios - Swift - UIAlertcontroller 中文本字段的大小

ios - 在 UIImage 上添加一层颜色

ios - 函数签名特化 <Arg[0] = Owned To Guaranteed>

objective-c - iOS 8 + Swift 中的 setHTTPBody