pete
pete

Reputation: 2046

self.presentedViewController won't clear even when it's dismissed, thus preventing interstitial AdMob ad from showing

Google gives a new error message "The provided view controller is already presenting another view controller" if self.presentedViewController exists. The problem is, self.presentedViewController seems to always exist even after dismissing itself!

code:

    print("printing presented view controller1: ")
    print(self.presentedViewController!)
    print("printing picker: ")
    print(picker)
    
    picker.dismiss(animated: true, completion: nil)

    print("printing presented view controller2: ")
    print(self.presentedViewController!)
    sleep(3)
    print("printing presented view controller3: ")
    print(self.presentedViewController!)
    
    // self.presentLoadingIndicator()
    
    if self.interstitial.isReady {
        print("presenting ad.")
        print("printing presented view controller: ")
        print(self.presentedViewController!)
        self.interstitial.present(fromRootViewController: self)
    } else {
      print("Ad wasn't ready")
    }

output:

printing presented view controller1: 
<UIImagePickerController: 0x7fe3c00c5c00>
printing picker: 
<UIImagePickerController: 0x7fe3c00c5c00>
printing presented view controller2: 
<UIImagePickerController: 0x7fe3c00c5c00>
printing presented view controller3: 
<UIImagePickerController: 0x7fe3c00c5c00>
presenting ad.
printing presented view controller: 
<UIImagePickerController: 0x7fe3c00c5c00>
2020-08-03 20:38:27.025517-0700 BeerVision[26634:3155513] <Google> The provided view controller is already presenting another view controller.
got into dismiss handler

As you can see, all of the printed view controller are the same object. How do we get rid of self.presentedViewController other than by dismissing? Shall I just forcibly point it to nil or something?

Upvotes: 2

Views: 886

Answers (1)

pete
pete

Reputation: 2046

Okay, I finally figured out that this might be an actual bug in Google's code because they assume the view isn't presentable as long as it has presentedViewController even though based on my debugging it could have a presentedViewController even after that presentedViewController was already dismissed (and in fact even after I presented a new view controller, somehow presentedViewController still shows the old one). Therefore I changed the version of my pod to a much older version of Google Ads. Here's what I did:

pod 'Google-Mobile-Ads-SDK', '7.53.1'

Then I was allowed to set the root view controller as "self" even when self has a dismissed presentedviewcontroller, and the ad shows up perfectly as expected.

There was still some finessing to make sure the ad doesn't get prematurely dismissed (unlike in Android apps, iOS interstitial ads will disappear if you kill their parent view, which is problematic if you wanted to display the ad while a "loading" progress bar is showing and the "loading" progress bar will eventually be dismissed while the ad is showing. I had to reprogram the code to avoid dismissing "loading" until the ad is not showing anymore)

Upvotes: 1

Related Questions