googleads / googleads-mobile-flutter

A Flutter plugin for the Google Mobile Ads SDK
Apache License 2.0
336 stars 275 forks source link

For iOS devices. ---The provided view controller is not being presented. #1146

Closed lcaifu closed 1 month ago

lcaifu commented 1 month ago

iOS device, InterstitialAd, when displaying A, to load B, after B loading is complete, close A at this time, and then display B will report an error. The provided view controller is not being presented.

Versions: google_mobile_ads: ^5.1.0

malandr2 commented 1 month ago

Hi @lcaifu, this is an iOS only error if the UIVIewController's view.window is nil. I think a minimal reproducible sample would help with your use case - can you provide one so I can take a closer look? Thanks

lcaifu commented 1 month ago

Hi @malandr2 ,This is my sample. Thanks

class AdTestPage extends StatelessWidget {
  const AdTestPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<AdTestLogic>();
    final state = Get.find<AdTestLogic>().state;

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(onPressed: logic.buttonA, child: Text("Button A")),
        ElevatedButton(onPressed: logic.buttonB, child: Text("Button B")),
      ],
    );
  }
}
class AdTestLogic extends GetxController {
  final AdTestState state = AdTestState();

  @override
  void onInit() {
    super.onInit();
    loadAdA();
  }

  void buttonA() {
    showA();
  }

  void buttonB() {
    showB();
  }

  InterstitialAd? appAdA;
  void loadAdA() {
    InterstitialAd.load(
        adUnitId: "ca-app-pub-3940256099942544/4411468910",
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          onAdLoaded: (InterstitialAd ad) {
            appAdA = ad;
            LogI('Test A load success ${appAdA?.hashCode}');
          },
          onAdFailedToLoad: (LoadAdError error) {
            appAdA?.dispose();
            appAdA = null;
            LogI('Test A load error ${appAdA?.hashCode}');
          },
        ));
  }

  void showA() {
    appAdA?.show();
    appAdA?.fullScreenContentCallback = FullScreenContentCallback(
        onAdShowedFullScreenContent: (ad) {
          LogI('Test A onAdShowedFullScreenContent ${appAdA?.hashCode}');
          loadAdB();
        },
        onAdImpression: (ad) {},
        onAdFailedToShowFullScreenContent: (ad, err) {
          LogI('Test A onAdFailedToShowFullScreenContent: $err ${appAdA?.hashCode}');
          appAdA?.dispose();
          appAdA = null;
        },
        onAdDismissedFullScreenContent: (ad) {
          LogI('Test A onAdDismissedFullScreenContent ${appAdA?.hashCode}');
          appAdA?.dispose();
          appAdA = null;
        },
        onAdClicked: (ad) {
          LogI('Test A onAdClicked ${appAdA?.hashCode}');
        });
  }

  InterstitialAd? appAdB;
  void loadAdB() {
    InterstitialAd.load(
        adUnitId: "ca-app-pub-3940256099942544/4411468910",
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          onAdLoaded: (InterstitialAd ad) {
            appAdB = ad;
            LogI('Test B load success ${appAdB?.hashCode}');
          },
          onAdFailedToLoad: (LoadAdError error) {
            appAdB?.dispose();
            appAdB = null;
            LogI('Test B load success ${appAdB?.hashCode}');
          },
        ));
  }

  void showB() {
    appAdB?.show();
    appAdB?.fullScreenContentCallback = FullScreenContentCallback(
        onAdShowedFullScreenContent: (ad) {
          LogI('Test B onAdShowedFullScreenContent ${appAdB?.hashCode}');
          loadAdA();
        },
        onAdImpression: (ad) {},
        onAdFailedToShowFullScreenContent: (ad, err) {
          LogI('Test B onAdFailedToShowFullScreenContent: $err ${appAdB?.hashCode}');
          appAdB?.dispose();
          appAdB = null;
        },
        onAdDismissedFullScreenContent: (ad) {
          LogI('Test B onAdDismissedFullScreenContent ${appAdB?.hashCode}');
          appAdB?.dispose();
          appAdB = null;
        },
        onAdClicked: (ad) {
          LogI('Test B onAdClicked ${appAdB?.hashCode}');
        });
  }
}

20240729-100315

malandr2 commented 1 month ago

Hi @lcaifu, your code is not reproducible for me to run, when copying it I run into errors related to classes that I don't have context to. Can you please share a minimal, reproducible example? Thanks

lcaifu commented 1 month ago

Hi @malandr2 , This is a new example code. Thanks

`import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart';

class TestAdPage extends StatefulWidget { @override State createState() => _TestAdPageState(); }

class _TestAdPageState extends State { @override void initState() { super.initState();

loadAdA();

}

void reinitialize() { loadAdA(); }

void buttonA() { showA(); }

void buttonB() { showB(); }

InterstitialAd? appAdA; void loadAdA() { InterstitialAd.load( adUnitId: "ca-app-pub-3940256099942544/4411468910", request: const AdRequest(), adLoadCallback: InterstitialAdLoadCallback( onAdLoaded: (InterstitialAd ad) { appAdA = ad; print('Test A load success ${appAdA?.hashCode}'); }, onAdFailedToLoad: (LoadAdError error) { appAdA?.dispose(); appAdA = null; print('Test A load error ${appAdA?.hashCode}'); }, )); }

void showA() { appAdA?.show(); appAdA?.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (ad) { print('Test A onAdShowedFullScreenContent ${appAdA?.hashCode}'); loadAdB(); }, onAdImpression: (ad) {}, onAdFailedToShowFullScreenContent: (ad, err) { print('Test A onAdFailedToShowFullScreenContent: $err ${appAdA?.hashCode}'); appAdA?.dispose(); appAdA = null; }, onAdDismissedFullScreenContent: (ad) { print('Test A onAdDismissedFullScreenContent ${appAdA?.hashCode}'); appAdA?.dispose(); appAdA = null; }, onAdClicked: (ad) { print('Test A onAdClicked ${appAdA?.hashCode}'); }); }

InterstitialAd? appAdB; void loadAdB() { InterstitialAd.load( adUnitId: "ca-app-pub-3940256099942544/4411468910", request: const AdRequest(), adLoadCallback: InterstitialAdLoadCallback( onAdLoaded: (InterstitialAd ad) { appAdB = ad; print('Test B load success ${appAdB?.hashCode}'); }, onAdFailedToLoad: (LoadAdError error) { appAdB?.dispose(); appAdB = null; print('Test B load success ${appAdB?.hashCode}'); }, )); }

void showB() { appAdB?.show(); appAdB?.fullScreenContentCallback = FullScreenContentCallback( onAdShowedFullScreenContent: (ad) { print('Test B onAdShowedFullScreenContent ${appAdB?.hashCode}'); }, onAdImpression: (ad) {}, onAdFailedToShowFullScreenContent: (ad, err) { print('Test B onAdFailedToShowFullScreenContent: $err ${appAdB?.hashCode}'); appAdB?.dispose(); appAdB = null; }, onAdDismissedFullScreenContent: (ad) { print('Test B onAdDismissedFullScreenContent ${appAdB?.hashCode}'); appAdB?.dispose(); appAdB = null; }, onAdClicked: (ad) { print('Test B onAdClicked ${appAdB?.hashCode}'); }); }

@override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton(onPressed: reinitialize, child: Text("Reinitialize")), ElevatedButton(onPressed: buttonA, child: Text("Click first A")), ElevatedButton(onPressed: buttonB, child: Text("Reclick B")), ], );; } } `

malandr2 commented 1 month ago

Hi @lcaifu, thanks for fixing the code, I'm able to run it.

I can reproduce your issue. This appears to be a Flutter issue as I cannot reproduce this using our Interstitial example iOS repo. I have escalated this to engineering to take a closer look. We are also working on #700 and the resolution may affect this issue. In the short-term, to mitigate the issue I called loadAdB() in onAdDismissedFullScreenContent and the problem went away.

lcaifu commented 1 month ago

你好@lcaifu,感谢您修复代码,我可以运行它了。

我可以重现您的问题。这似乎是 Flutter 的问题,因为我无法使用我们的 Interstitial 示例 iOS 存储库重现此问题。我已将此问题上报给工程部门以进行更深入的研究。我们也在处理#700,解决方案可能会影响此问题。在短期内,为了缓解问题,我打电话咨询loadAdB()onAdDismissedFullScreenContent问题就解决了。

Thanks,This solution can be used temporarily.

malandr2 commented 1 month ago

This has been resolved with #1153 and will be included in the next google_mobile_ads plugin release