我有一个UIAlertController,其中一个按钮名为“Settings”。点按即可进入设置App中的特定部分(例如蓝牙)。我在这里看到了很多解决方案,但它们都不适用于iOS 11。
这是我的代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
let alertView = UIAlertController(title: "Info", message: "message", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "Settings", style: .cancel, handler: { (alert) in
UIApplication.shared.open(URL(string:"App-Prefs:root=Settings&path=General")!, options: [:], completionHandler: nil)
}))
self.present(alertView, animated: true, completion: nil)
}
}
我还在App-> Target-> Info-> URLTypes-> URL Schemes:“App-Prefs”下添加了。我也试过“prefs”但是什么都没有用。
有没有人有iOS 11的解决方案?任何帮助都非常感谢
你只需要尝试没有App-Prefs的UIApplicationOpenSettingsURLString
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
let url = UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}