如何在设置中打开您的应用 iOS 11

How to open your app in Settings iOS 11

苹果好像把很多app的配置都移到了App路径下iOS 11、如何在设置中以编程方式打开app路径?我试过 "App-Prefs:root=\(Bundle.main.bundleIdentifier!)" 但这似乎不起作用。

Please note that my question is specific to: How to open the app path in settings: NOT how to open the settings

我猜这是您要查找的代码:

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

此外,swift 5 的更新版本:

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

openURL 自 iOS 10 以来已被弃用,因此我建议您使用:

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)")
        })
    }
}

如果你想让它同时适用于新旧 iOS 版本,请执行以下操作:

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

Swift 4.2, iOS 12

使用以下函数可以只打开设置:

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
    }
}

用法:UIApplication.openAppSettings()

但注意不要使用“非publicURL方案”,例如: prefs:root=App-Prefs:root,否则您的应用将被拒绝。这最近发生在我身上,因为我试图在设置中深入链接到 wifi 部分。

UIApplicationOpenSettingsURLString 已重命名为 UIApplication.openSettingsURLString

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

SWift 5

在某些情况下,我们在尝试上述所有方法后仍无法打开应用程序的设置。要 100% 解决这个问题,只需确保遵循这两个步骤

步骤 1. 右击项目文件 -> 添加新文件 -> 在项目中添加 Settings.Bundle 并根据您的要求进行编辑。

第 2 步。现在在按钮操作中添加一些代码。

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

注意:苹果禁止使用“prefs:Root”,您的应用将被拒绝。所以,避免使用这个 api.