iOS 的 Xamarin 表单和 Firebase AdMob

Xamarin forms and Firebase AdMob for iOS

几天前,我的应用程序运行良好,直到我在 Output 中收到一条消息,建议我添加以下行:

request.TestDevices = new[] { "yourtestcodeid" };

对于 Xamarin 组件商店,名为 Xamarin.Google.iOS.MobileAds 的组件不再存在,但您必须为 [= 安装 Firebase AdMob 34=].

问题是该示例仅适用于原生 iOS 项目。我在主项目中有一个带有通用视图的 Xamarin 表单项目

public class AdMobView : ContentView {
    /// <summary>
    /// Initializes a new instance of the <see cref="AdMobView"/> class.
    /// </summary>
    public AdMobView() {
    }
}

并在每个平台的项目中实现。对于 iOS 是:

[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobRenderer))]
namespace WordBankEasy.iOS.Renderers {
public class AdMobRenderer : ViewRenderer {
    const string AdmobID = "ca-app-pub-yourcode";

    BannerView adView;
    BannerView adViewTableView;
    BannerView adViewWindow;
    Interstitial adInterstitial;
    bool viewOnScreen;

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e) {
        base.OnElementChanged(e);

        if(e.NewElement == null)
            return;

        if(e.OldElement == null)
        {
            adView = new BannerView(
                size: AdSizeCons.SmartBannerPortrait, 
                origin: new CGPoint(0, UIScreen.MainScreen.Bounds.Size.Height - AdSizeCons.Banner.Size.Height))
            {
                AdUnitID = AdmobID,
                RootViewController = UIApplication.SharedApplication.Windows[0].RootViewController
            };

            adView.AdReceived += (sender, args) =>
            {
                if(!viewOnScreen) this.AddSubview(adView);
                viewOnScreen = true;
            };

            adView.LoadRequest(GetRequest());
            base.SetNativeControl(adView);
        }
    }

    private void AdViewWindow_AdReceived(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    Request GetRequest()
    {
        var request = Request.GetDefaultRequest();
        // Requests test ads on devices you specify. Your test device ID is printed to the console when
        // an ad request is made. GADBannerView automatically returns test ads when running on a
        // simulator. After you get your device ID, add it here
        request.TestDevices = new[] { "96080e40efec5229aad21b540eae6fe0" };
        return request;
    }
}
}

现在我无法查看广告,但我遇到了一种新的错误

You must set the rootViewController property of <GADBannerView: 0x12de69c50; frame = (0 617; 375 50); clipsToBounds = YES; layer = <CALayer: 0x17103da00>> before loading a request.

如何更改我的代码?预先感谢您的任何建议。

对于新组件,需要对代码进行一些改动,例如:

if(e.OldElement == null)
{
    UIViewController viewCtrl = null;

    foreach(UIWindow v in UIApplication.SharedApplication.Windows)
    {
        if (v.RootViewController != null)
        {
            viewCtrl = v.RootViewController;
        }
    }

    adView = new BannerView(size: AdSizeCons.Banner, origin: new CGPoint(-10, 0))
    {
        AdUnitID = AdmobID,
        RootViewController = viewCtrl
    };

    adView.AdReceived += (sender, args) =>
    {
        if(!viewOnScreen) this.AddSubview(adView);
        viewOnScreen = true;
    };

    adView.LoadRequest(GetRequest());
    base.SetNativeControl(adView);
}