0

I have implemented the code below but i have a warning message:

Warning: Attempt to present on whose view is not in the window hierarchy! on paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue)

and the alert is not displayed. [edit]If I rotate the phone, the alert is displayed just after the rotation [/edit]

Here is my code:

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
    alert("Félicitations", message: "Vous avez restauré vos packs, cliquez sur ok pour les télécharger !")

}

func alert(title:String, message:String = "") {
    let alert = UIAlertController(title: title, message:message, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in
        if title == "Félicitations" {
            let next = self.storyboard?.instantiateViewControllerWithIdentifier("TelechargementVC") as! TelechargementViewController
            self.presentViewController(next, animated: true, completion: nil)

        }

        })
    self.presentViewController(alert, animated: true){}

}

NB : The alert function is called from other functions without problem.

How can I solve this?

Ludo
  • 743
  • 1
  • 10
  • 25
  • I have tried to show a new view from paymentQueueRestoreCompletedTransactionsFinished and got the same kind of message... – Ludo Jun 16 '16 at 15:25

1 Answers1

0

Thanks to this helpfull post: https://stackoverflow.com/a/26667122/5030969 I have changed the alert function to get the the topmost view controller and make the presentViewController from there

func alert(title:String, message:String = "") {

    if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        // topController should now be your topmost view controller
        let alert = UIAlertController(title: title, message:message, preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in
            if title == "Félicitations" {
                let next = self.storyboard?.instantiateViewControllerWithIdentifier("TelechargementVC") as! TelechargementViewController
                topController.presentViewController(next, animated: true, completion: nil)

            }
        })

        topController.presentViewController(alert, animated: true){}

    }

}
Community
  • 1
  • 1
Ludo
  • 743
  • 1
  • 10
  • 25