在设置应用中打开应用的通知设置

Opening app's notification settings in the settings app

如果用户可能不小心拒绝接收通知并想稍后转通知,我如何使用 NSURL 打开 IOS 设置应用程序到我应用程序的通知页面,在那里他们可以 select 允许通知?

对于 Swift 3,使用 UIApplicationOpenSettingsURLString 转到您的应用的设置,其中显示通知状态和 "Cellular Data"

let settingsButton = NSLocalizedString("Settings", comment: "")
let cancelButton = NSLocalizedString("Cancel", comment: "")
let message = NSLocalizedString("Your need to give a permission from notification settings.", comment: "")
let goToSettingsAlert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.alert)

goToSettingsAlert.addAction(UIAlertAction(title: settingsButton, style: .destructive, handler: { (action: UIAlertAction) in
    DispatchQueue.main.async {
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            } else {
                UIApplication.shared.openURL(settingsUrl as URL)
            } 
        }
    }
}))

logoutUserAlert.addAction(UIAlertAction(title: cancelButton, style: .cancel, handler: nil))

更新:这将被 Apple 拒绝。

要打开部分设置的通知,请使用此

UIApplication.shared.open(URL(string:"App-Prefs:root=NOTIFICATIONS_ID")!, options: [:], completionHandler: nil)

2021 年 12 月 8 日更新:

此方法将打开“设置”>“您的应用”。它将显示所有可用的隐私开关,如相机、照片、通知、蜂窝数据等。

在下面@Mischa 发表评论后,测试并更新了对此的答案(更简洁):

if let appSettings = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
    UIApplication.shared.open(appSettings)
}

上一个回答:

我发现这个问题的答案(尽管有帮助)有太多假定的逻辑。这是一个简单明了的 Swift 5 实现,如果其他人偶然发现了这个问题:

if let bundleIdentifier = Bundle.main.bundleIdentifier, let appSettings = URL(string: UIApplication.openSettingsURLString + bundleIdentifier) {
    if UIApplication.shared.canOpenURL(appSettings) {
        UIApplication.shared.open(appSettings)
    }
}

Swift 5:

if let url = URL(string: UIApplication.openSettingsURLString) {
    UIApplication.shared.open(url)
}