Displaying admob interstitals multiple times how?

2019-07-07 00:23发布

I have small gaming app which has one storyboard and inside it creates scenes like start menu-gamin area-scores I have added admob banner view and interstitals into it.My banner view is working fine and but my interstitial only works for one time.

I load my interstitial on my viewdidload and fire it in the function which calls the gaming sessions end and as I say it works but only for one time when user starts another game and fails this time there is no interstital(error below).So what should I do to fix it I want my game to show interstitials multiple times whenever I want.

Error : Request Error: Will not send request because interstitial object has been used.

Header:

#import "GADBannerView.h"
#import "GADInterstitial.h"
@class GADInterstitial;
@class GADRequest;
////////////code UIviewcontroller//////////
        GADBannerView *bannerView_;
        GADInterstitial *interstitial_;

Implementation

-(void)viewdidload
{
//////////////////gaming code///////////

interstitial_ = [[GADInterstitial alloc] init];
    interstitial_.delegate = self;

    interstitial_.adUnitID = @"ca-app-pub-6280395701552972/5217388242";
    GADRequest *request = [GADRequest request];
    [interstitial_ loadRequest:request];

}

Implementaion

-(void)failgame
{
//////////////////gaming code///////////

    [interstitial_ presentFromRootViewController:self];

}

On googleadmob SDK page it says that interstials are one time use objects so I am %100 sure that is the problem but there is nothing there to explain how to call them multiple time's so as long as you point the answer please don't tell go read it I have read it 5 times.

3条回答
老娘就宠你
2楼-- · 2019-07-07 00:36
- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [self createAndLoadInterstitial];
}

- (GADInterstitial *)createAndLoadInterstitial {
  GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
  interstitial.delegate = self;
 [interstitial loadRequest:[GADRequest request]];
  return interstitial;
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
  self.interstitial = [self createAndLoadInterstitial];
}

GADInterstitial is a one time use object. To request another interstitial, you'll need to allocate a new GADInterstitial object.

The best place to allocate another interstitial is in the interstitialDidDismissScreen: method on GADInterstitialDelegate, so that the next interstitial starts loading as soon as the previous one is dismissed. reference: admob site link

查看更多
够拽才男人
3楼-- · 2019-07-07 00:52

Well no one gave an answer but I am sure that there are some others out there who had the same problem so for those who want to call interstitals multiple times here is the trick.

Put it into its own method and call method from your main method (which replies many times)

Keep the interstitals on your viewdidload (or where you want fire first) because if you don't then you miss the first fire the others will work.

The full code for that.

- (void) callint
{
    int rNumber1 = arc4random() % 45 + 1;
    int rNumber2 = arc4random() % 45 + 1;
    if((rNumber1%2==1) && (rNumber1%1==0))
    {
    [interstitial_ presentFromRootViewController:self];
    interstitial_ = [[GADInterstitial alloc] init];
    interstitial_.delegate = self;
    interstitial_.adUnitID = @"ca-app-pub-6280395701552972/5217388242";
    GADRequest *request = [GADRequest request];
    [interstitial_ loadRequest:request];

    }

}

I put random number creation and if there because I don't want users to see those intersitials every time even call int is fired every time there is 4/1 chance for it to fire so it shows 1 interstitial for 4-5 fire.

查看更多
小情绪 Triste *
4楼-- · 2019-07-07 00:53

If you follow the Admob recommended code, as Sonu VR showed, you will have no issue. Just note that whilst there will be a logging error despite of using the best practice Admob code, that is a red herring. That's probably a bug in the Admob logging, but not a bug in the Admob code to display an Ad. You can test for this by using a non-test phone and a different ad will be served at different times.

查看更多
登录 后发表回答