xcode 在设置应用程序中打开应用程序的通知设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42848539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:50:51  来源:igfitidea点击:

Opening app's notification settings in the settings app

iosswiftxcodeapplication-settings

提问by Matt

In the case that a user may accidentally declines to receive notifications and wants to turn notifications later, how can I use an NSURL to open the IOS Settings App to my app's notification page where they can select Allow Notifications?

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

回答by CodeOverRide

For Swift 3, use UIApplicationOpenSettingsURLStringto go to settings for your app where it shows the Notifications status and "Cellular Data"

对于 Swift 3,用于UIApplicationOpenSettingsURLString转到您的应用程序的设置,其中显示通知状态和“蜂窝数据”

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

回答by Gligor

I found the answer to this question (albeit helpful) has a bit too much assumed logic. Here is a plain and simple Swift 5 implementation if anyone else stumbles upon this question:

我发现这个问题的答案(虽然有帮助)有一些假设的逻辑。如果其他人偶然发现这个问题,这里有一个简单明了的 Swift 5 实现:

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

回答by ttorbik

UPDATE:This will be rejected by Apple.

更新:这将被苹果​​拒绝。

To open notifications part of settings use this

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

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