Opening app's notification settings in the settings app

前端 未结 3 934
心在旅途
心在旅途 2020-12-18 21:13

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 a

相关标签:
3条回答
  • 2020-12-18 21:40

    For Swift 3, use UIApplicationOpenSettingsURLString to go to settings for your app where it shows the Notifications status and "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))
    
    0 讨论(0)
  • 2020-12-18 22:00

    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:

    if let bundleIdentifier = Bundle.main.bundleIdentifier, let appSettings = URL(string: UIApplication.openSettingsURLString + bundleIdentifier) {
        if UIApplication.shared.canOpenURL(appSettings) {
            UIApplication.shared.open(appSettings)
        }
    }
    
    0 讨论(0)
  • 2020-12-18 22:03

    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)
    
    0 讨论(0)
提交回复
热议问题