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?
Updated 8 Dec, 2021:
This method will open Settings > Your App. It will show all available privacy toggles like camera, photos, notifications, cellular data, etc.
After a comment from @Mischa below, tested and updated the answer to this (more succinct):
if let appSettings = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
Previous answer:
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)
}
}
bundleIdentifier
to the actual settings URL. Is there an actual reason for that? The URL created from UIApplication.openSettingsURLString
opens the app settings as well and it seems obsolete. In other words: The simplest solution is Eric Yuan's answer. Apple's documentation of the property states the same: "When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any. Use this string with open(_:options:completionHandler:)
." –
Cream 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))
UIApplication.openSettingsURLString
. –
Gantline This should keep you covered for all iOS versions, including iOS 15 and iOS 16.
extension UIApplication {
private static let notificationSettingsURLString: String? = {
if #available(iOS 16, *) {
return UIApplication.openNotificationSettingsURLString
}
if #available(iOS 15.4, *) {
return UIApplicationOpenNotificationSettingsURLString
}
if #available(iOS 8.0, *) {
// just opens settings
return UIApplication.openSettingsURLString
}
// lol bruh
return nil
}()
private static let appNotificationSettingsURL = URL(
string: notificationSettingsURLString ?? ""
)
func openAppNotificationSettings() -> Bool {
guard
let url = UIApplication.appNotificationSettingsURL,
self.canOpen(url) else { return false }
return self.open(url)
}
}
Usage:
Button {
let opened = UIApplication.shared.openAppNotificationSettings()
if !opened {
print("lol fail")
}
} label: {
Text("Notifications")
}
Swift 5:
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
Since iOS 15.4 we can deeplink to the notif settings screen directly using UIApplicationOpenNotificationSettingsURLString
:
openNotificationSettingsURLString
–
Aswan 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)
you can use this
if #available(iOS 16.0, *) {
if let appSettings = URL(string: UIApplication.openNotificationSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
} else {
if let appSettings = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
You can navigate to the Notification settings in macOS using this function. However, please note that you will need to manually scroll to locate your app within the settings: func openNotificationsSettings() { UIApplication.shared.open(URL(string:"x-apple.systempreferences:com.apple.preference.notifications")!, options: [:], completionHandler: nil) }
© 2022 - 2024 — McMap. All rights reserved.
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
– Pulsimeter