微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试放置 AdMob

如何解决尝试放置 AdMob

我尝试在我的 xamarin iOS 项目中放置一些来自 AdMob 的横幅。我休耕这个步骤:

Xamarin Forms with Google Admob tutorial

  1. 我在 App.xaml.cs 文件中将 MainPage = new MainPage(); 更改为 MainPage = new NavigationPage(new MainPage());

  2. 我在我的基础项目上创建了新文件Controls,并在那里我用他的代码创建了新类 AdMobControl.cs

使用系统;

using Xamarin.Forms;

using static WeatherLocationInfo.Controls.AdMobControl;

namespace WeatherLocationInfo.Controls
{

public class AdMobControl : View
{
    public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
                   nameof(AdUnitId),typeof(string),typeof(AdMobControl),string.Empty);

    public string AdUnitId
    {
        get => (string)GetValue(AdUnitIdProperty);
        set => SetValue(AdUnitIdProperty,value);
    }
}
} 
  1. 我在我的基础项目上创建新文件并使用以下代码将其命名为 AppConstants.cs

    使用系统;

     using Xamarin.Forms;
    
     namespace WeatherLocationInfo
     {
    
     public class AppConstants
     {
         public static string AppId
         {
             get
             {
                 switch (Device.RuntimePlatform)
                 {
                     case Device.Android:
                         return "";
                     default:
                         return "";
                 }
             }
         }
    
         /// <summary>
         /// These Ids are test Ids from https://developers.google.com/admob/android/test-ads
         /// </summary>
         /// <value>The banner identifier.</value>
         public static string BannerId
         {
    
             get
             {
                 switch (Device.RuntimePlatform)
                 {
                     case Device.iOS:
                         return "ca-app-pub-3940256099942544/6300978111";
                     default:
                         return "ca-app-pub-3940256099942544/6300978111";
                 }
             }
         }
    
         /// <summary>
         /// These Ids are test Ids from https://developers.google.com/admob/android/test-ads
         /// </summary>
         /// <value>The Interstitial Ad identifier.</value>
         public static string InterstitialAdId
         {
             get
             {
                 switch (Device.RuntimePlatform)
                 {
                     case Device.Android:
                         return "ca-app-pub-3940256099942544/1033173712";
                     default:
                         return "ca-app-pub-3940256099942544/1033173712";
                 }
             }
         }
    
         public static bool ShowAds
         {
             get
             {
                 _adCounter++;
                 if (_adCounter % 5 == 0)
                 {
                     return true;
                 }
                 return false;
             }
         }
    
         private static int _adCounter;
    
     }
    

    }

  2. 在我的 MainPage.xaml.cs 中,我在 InitializeComponent();添加了以下代码

    AdMobControl admobControl = new AdMobControl()

         {
    
             AdUnitId = AppConstants.BannerId
         };
         Label adLabel = new Label() { Text = "Ads will be displayed here!" };
    

    Content = new StackLayout() { 儿童 = {adLabel,admobControl} };

             this.Title = "Admob Page";
    
  3. 我在我的 iOS 项目中添加了来自 Nuget 的新包 -> Xamarin.Google.iOS.MobileAds

  4. 在我的 AppDelegate.cs 中,我添加了此代码 -> Google.MobileAds.MobileAds.Configure(AppConstants.AppId);

之后我尝试在调试模式下运行项目,但收到此错误

Runtime.cs file not found

Runtime.cs file not found

有没有办法解决这个问题?

P.S 很抱歉,代码的一部分没有放在撇号中,但是当我复制代码并且编辑器自己没有将其放在撇号中时

解决方法

您是否更新了 info.plist 文件?

    <key>GADApplicationIdentifier</key>
    <string>YOUR APP ID</string>
    <key>GADIsAdManagerApp</key>
    <true/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。