Displaying admob interstitals multiple times how?

一个人想着一个人 提交于 2020-01-03 17:26:29

问题


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.


回答1:


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.




回答2:


- (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:


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.



来源:https://stackoverflow.com/questions/24353456/displaying-admob-interstitals-multiple-times-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!