Je veux afficher une alerte lorsque la notification est lancée alors que l'application est en cours d'exécution, voici tutorial que j'ai utilisé pour les notifications locales. Dans tutoriel, il utilise UIAlertView
pour montrer alerte et cela fonctionne, voici le code:Swift: Afficher UIAlertController dans la méthode didReceiveLocalNotification
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Point for handling the local notification when the app is open.
// Showing reminder details in an alertview
UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
}
mais il avertit que UIAlertView est dépréciée dans iOS 9, donc j'utilisé UIAlertController
, mais quand je le lance, il avertit:
Attempt to present <UIAlertController: 0x7c322a00> on <xxx> whose view is not in the window hierarchy!
Voici ma méthode didReceiveLocalNotification
:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in
}))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
Comment je peux montrer UIAlertController dans la méthode didReceiveLocalNotification
? J'ai essayé aussi:
let activeViewCont = application.windows[0].rootViewController?.presentedViewController
activeViewCont!.presentViewController(alert, animated: true, completion: nil)
'self.window.rootViewController' est-ce un contrôleur de navigation dans votre projet? – Nishant
@Nishant non, je n'ai pas utilisé le contrôleur de navigation sur mon projet. 'self.window.rootViewController' renvoie mon SplashScreen où j'ai créé mon propre écran de démarrage, il télécharge des données lorsque l'application s'ouvre pour la première fois. Mais j'ai besoin d'obtenir le contrôleur de vue en cours – kakajan