I created an app with a calendar to store events and a reminder. When the app starts, it asks for permission, but if the user does not accept it, the notifications do not start.
I made it so that when the user creates a new event, a modal appears warning him that he has not accepted notifications.
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("ok")
} else {
print("ko")
}
}
}
}
If the user taps the popup he is taken to the settings screen ios to activate notifications.
if let url = URL(string:UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Then he goes back to my app but the notifications still don’t arrive, because the user would have to restart my app to have the settings changed, because the code is found in AppDelegate which is loaded only once when the app is started.
Is there a way to avoid having to have the user restart the app? Is it possible that Apple hasn’t foreseen a use case where the user changes their mind about giving permission to notifications?
3