0

我在获取 GADInterstitial 自定义广告时遇到问题我尝试过此代码

if(nil != m_pBannerView)
{
    m_pBannerView.delegate = nil;
    [m_pBannerView release];
    m_pBannerView = nil;
}
m_pBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
m_pBannerView.delegate = self;
m_pBannerView.rootViewController = self;
m_pBannerView.adUnitID = @"AdMob Publisher ID";
m_pBannerView.rootViewController = self;
[self.view addSubview:m_pBannerView];

GADRequest *request = [GADRequest request];
request.testing = YES;
[m_pBannerView loadRequest:request];

if(nil != m_pInterstitial)
{
    [m_pInterstitial release];
    m_pInterstitial = nil;
}

m_pInterstitial = [[GADInterstitial alloc] init];
m_pInterstitial.delegate = self;
m_pInterstitial.adUnitID = @"INTERSTITIAL_AD_UNIT_ID";

GADRequest *interstialRequest = [GADRequest request];
interstialRequest.testing = YES;
[m_pInterstitial loadRequest: interstialRequest];

在 GADInterstitial Delegates 中,我正在调用 [ad presentFromRootViewController:self];

但我仍然无法获得自定义广告,请帮助我。

4

2 回答 2

0

您必须使用自己独特idadUnitID财产

于 2012-08-16T09:54:33.630 回答
0

GADInterstitial是一种在您的应用程序中展示广告的有趣方式,也是一种棘手的方式。按照这个例子,让我们进行以下步骤:

  • 首先,我们需要为它们设置显示环境。下载GoogleAdMobAdsSdkiOS,最好是最新的。将 SDK 添加到您的项目中,但请记住删除 SDK 中 AddOns 文件夹中的示例项目。

  • 接下来,在您的项目>>构建阶段>>链接二进制与库中添加以下框架:

    • AdSupport.framework(如果适合< iOS7,请选择可选)
    • StoreKit.framework
    • CoreData.framework
    • CoreAudio.framework
    • AVFoundation.framework
    • MessageUI.framework
    • AudioTool.framework
    • libGoogleAdMobAds.a(位于 SDK 文件夹中)
  • 基础已经完成。现在我们需要选择我们希望在其中看到广告的 ViewController。所以这里有一些代码,这里我们导入 GADInterstitialDelegate 的标头并使用 MainViewController.h 扩展它:

   #import "GADInterstitialDelegate.h"
   #define kSampleAdUnitID @"/6253334/dfp_example_ad/interstitial"

   @class GADInterstitial;
   @class GADRequest;

   @interface MainViewController : UIViewController<GADInterstitialDelegate>
   {   
         BOOL isLoaded_;   
         NSTimer *quickie_;
   }

   @property(nonatomic, strong) GADInterstitial *interstitial;

   //Make sure the delegate is handled properly.
  • 现在我们需要转到 MainViewController.m 中的实现:

   - (void)viewDidLoad
   {
       [super viewDidLoad];
       // Do any additional setup after loading the view.

       // FOLLOW THE CODE BELOW FOR ADMOD INTERESTIAL IMPLEMENTATION
       [self initializeAds];
       quickie_ = [[NSTimer alloc] init];
       quickie_ = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showAdd) userInfo:nil repeats:YES];
   }


   -(void)showAdd
   {   
       if(isLoaded_ && [quickie_ isValid])
       {
           [quickie_ invalidate];
           quickie_ = nil;
           [self.interstitial presentFromRootViewController:self];
       }
   }

   - (GADRequest *)request
   {
       GADRequest *request = [GADRequest request];
       return request;
   }

   -(void)initializeAds
   {
       // Create a new GADInterstitial each time.  A GADInterstitial will only show one request in its
       // lifetime. The property will release the old one and set the new one.
       self.interstitial = [[GADInterstitial alloc] init];
       self.interstitial.delegate = self;

       // Note: kSampleAdUnitId is a staticApId for example purposes. For personal Ads update kSampleAdUnitId with your interstitial ad unit id.
       self.interstitial.adUnitID = kSampleAdUnitID;
       [self.interstitial loadRequest:[self request]];
   }

   - (void)viewWillLayoutSubviews
   {
       [super viewWillLayoutSubviews];
       self.loadingSpinner.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, self.loadingSpinner.center.y);
   }

   - (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial
   {
       isLoaded_ = YES;
   }

   - (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error
   {
       isLoaded_ = NO;
   }

   //----ADS IMPLEMENTATION TILL HERE--->//

此处的计时器“quickie_”不断检查广告是否已成功加载,当成功加载时,如果用户仍在其上,它会在 ViewController 上拍摄广告。静态 kSampleAdUnitID 是一个始终有效的 sampleId。而已。运行您的代码并在您选择的 ViewController 上找到您的插页式广告。希望我有所帮助。干杯! :)

于 2014-05-06T23:52:12.563 回答