capacitor-community / admob

Community plugin for using Google AdMob
MIT License
205 stars 66 forks source link

iOS Error with requestConsentInfo - No ViewController #277

Closed kisimediaDE closed 8 months ago

kisimediaDE commented 10 months ago

Describe the bug Ask consent on iOS via the code

  async showConsent() {
    const consentInfo = await AdMob.requestConsentInfo();

    if (
      consentInfo.isConsentFormAvailable &&
      consentInfo.status === AdmobConsentStatus.REQUIRED
    ) {
      const { status } = await AdMob.showConsentForm();
      console.log(status);
    }
  }

I get the following message in Xcode when I try to run the app:

⚡️  TO JS {"isConsentFormAvailable":true,"status":"REQUIRED"}
⚡️  To Native ->  AdMob showConsentForm 94268627
ERROR MESSAGE:  {"message":"No ViewController","errorMessage":"No ViewController"}

The app is already deployed to the App Store but no Consent Window is shown and the app don't show ads anymore.

Adding this part did not make difference:

  const consentInfo = await AdMob.requestConsentInfo({
    debugGeography: AdmobConsentDebugGeography.EEA,
    testDeviceIdentifiers: ['YOUR_DEVICE_ID']
  });

To Reproduce Steps to reproduce the behavior:

  1. Create an Capacitor app with AdMob
  2. Add consent code from README.MD
  3. Start the app in Xcode
  4. See error

Expected behavior The consent window to show and then display ads

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context "@capacitor-community/admob": "^5.1.1"

kisimediaDE commented 10 months ago

I tried to remove ios folder and then run npx cap add ios again. but same error message. (instead of only do a sync)

LirycMoaners commented 9 months ago

Same issue for my app

LirycMoaners commented 9 months ago

By looking at the code it seems that the reason is the following function returning nil func getRootVC() -> UIViewController? { var window: UIWindow? = UIApplication.shared.delegate?.window ?? nil if window == nil { let scene: UIWindowScene? = UIApplication.shared.connectedScenes.first as? UIWindowScene window = scene?.windows.filter({$0.isKeyWindow}).first if window == nil { window = scene?.windows.first } } return window?.rootViewController }

bonny commented 9 months ago

I had the same problem and I solved it like below:

In my case I thought I was supposed to call AdMob.requestConsentInfo() and AdMob.showConsentForm() first and the depending on the response finally call AdMob.initialize(). But it seems like you first need to call AdMob.initialize() and then the consent stuff.

Not sure I'm doing it right but then it worked for me. I moved down my logic so I only call AdMob.showBanner() when a consent is obtained, so no banners is shown before consent is made.

rdlabo commented 8 months ago

Thanks for issue.Perhaps there is a problem with the order of method calls. Please refer to the following to execute.

https://github.com/capacitor-community/admob/commit/21d0302426cafcc2464203cb9828dd31535bf2fb

kisimediaDE commented 8 months ago

@rdlabo thanks for the update! Tested it now and everything is working well. THANK YOU!!!!