How to open your app in Settings iOS 11

偶尔善良 提交于 2019-11-30 17:05:27
Damien Bannerot

Here is the code you're looking for, I guess:

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

Swift 4.2, iOS 12

Opening just the settings is possible with the function below:

extension UIApplication {

    ...

    @discardableResult
    static func openAppSettings() -> Bool {
        guard
            let settingsURL = URL(string: UIApplication.openSettingsURLString),
            UIApplication.shared.canOpenURL(settingsURL)
            else {
                return false
        }

        UIApplication.shared.open(settingsURL)
        return true
    }
}

Usage: UIApplication.openAppSettings()

But be careful to NOT use "non-public URL scheme", such as: prefs:root= or App-Prefs:root, because otherwise your app will be rejected. This happened to me recently since I was trying to have a deeplink to the wifi section in the settings.

And if you want to make it work for both, older and newer iOS-versions, then do:

if let url = URL(string:UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}
skornos

openURL has been deprecated since iOS 10, so I would advise you to use:

if let url = URL(string:UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: { success in
            log.debug("Open app settings success: \(success)")
        })
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!