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

时间:2017-03-17 03:09:58

标签: ios swift xcode application-settings

如果用户可能会意外拒绝接收通知并希望稍后再发送通知,我该如何使用NSURL将IOS设置应用程序打开到我应用的通知页面,在那里他们可以选择允许通知?

4 个答案:

答案 0 :(得分:24)

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

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

答案 1 :(得分:9)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

斯威夫特 5:

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