Search code examples
iosswiftxcodefacebook-audience-networkreplaykit

Why cant I show my facebook interstitial ad after dismissing a view controller?


Im working with ReplayKit and when I press the cancel or save button it dismisses the view controller and I would like to show an ad. The problem is that the ad doesn't show up and I get this error: How do I fix it? Thanks!

Warning: Exception caught during invocation of received message, dropping incoming message and invalidating the connection. Exception: This method must be called on the main thread

internal func previewControllerDidFinish(previewController: RPPreviewViewController) {

previewViewController.dismissViewControllerAnimated(true, completion: nil)

NSNotificationCenter.defaultCenter().postNotificationName("loadAd", object: nil)

}

Solution

  • All UI related operations must be done on the main thread. You could do something like this using GCD:

    dispatch_async(dispatch_get_main_queue()) {
            previewViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    

    You could also post the notification in the completion block of dismissViewControllerAnimated