2016-02-27 1 views
1

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) 
+0

'self.window.rootViewController' est-ce un contrôleur de navigation dans votre projet? – Nishant

+0

@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

Répondre

9

Essayez ceci:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 

    var topController : UIViewController = (application.keyWindow?.rootViewController)! 

    while ((topController.presentedViewController) != nil) { 

     topController = topController.presentedViewController! 
    } 

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in})) 

    topController.presentViewController(alert, animated: true, completion: nil) 

}) 

Hope it helps.

+0

j'ai également essayé cela avant, cela n'aide pas trop. il donne la même erreur: 'Avertissement: Tentative de présenter sur dont la vue n'est pas dans la hiérarchie de la fenêtre!' – kakajan

+0

Je pense que je dois trouver le contrôleur de vue actuel, mais je ne sais pas comment, par exemple, quand j'obtiens cet avertissement, l'application n'affichait pas d'écran de démarrage, elle montrait un autre contrôleur de vue – kakajan

+0

Essayez ceci http://stackoverflow.com/a/12684721/1463604 – Nishant