gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:51:12 26 4
gpt4 key购买 nike

在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com