SpriteKit中的AdMob插页式广告

时间:2014-10-25 09:20:58

标签: ios objective-c admob

我以前曾使用过AdMob来展示横幅广告,而这个广告正在这个项目中使用 现在,我不会在游戏结束后显示AdMob广告 这是用SpriteKit编写的游戏。

在比赛失败后的我的GameScene中,我做了:

GameOverScene* gameOverScene = [[GameOverScene alloc] initWithSize:self.frame.size playerScore:_topPoints playerTime:elapsedTime];
SKTransition *transition = [SKTransition fadeWithDuration:2.5];
[self.view presentScene:gameOverScene transition:transition];

这很好。

在GameOverScene.m中我有:

@interface GameOverScene ()
@property(nonatomic, strong) GADInterstitial *interstitial; // for Ads
@end

@implementation GameOverScene

-(id)initWithSize:(CGSize)size playerScore:(NSUInteger)score playerTime:(CFTimeInterval)elapsedTime;
{
    self = [super initWithSize:size];

    if (self)
    {
        // code for hight score just text label, not important

        // for adds
        [self performSelector:@selector(showBanner) withObject:nil afterDelay:1.5];
    }

    return self;
}

- (void) showBanner
{
    self.interstitial = [[GADInterstitial alloc] init];
    self.interstitial.adUnitID = @"ca-app-pub-3940256099942544/4411468910";

    GADRequest *request = [GADRequest request];
    // Requests test ads on simulators.
    request.testDevices = @[ GAD_SIMULATOR_ID ];
    [self.interstitial loadRequest:request];

    [self performSelector:@selector(showBanner1) withObject:nil afterDelay:1.5];
}

- (void) showBanner1
{
    if ([self.interstitial isReady])
    {
        NSLog(@"EEEEEEEEEEEEEEEEE");

        [self.interstitial presentFromRootViewController:(UIViewController *)self];
    }
    else
    {
        NSLog(@"NO NO NO NO AD");
        [self.interstitial presentFromRootViewController:self];
    }
}

此代码正在执行,但我遇到以下问题:

[self.interstitial presentFromRootViewController:(UIViewController *)self];

运行时错误:

-[GameOverScene presentViewController:animated:completion:]: unrecognized selector sent to instance 0x7fee8b8ceb80

据我所知,我认为存在一些问题,因为self.interstitial presentFromRootViewController:期待RootViewController,但我的GameOverScene是SKScene。

问题
如何在SKScene中显示AdMob插页式广告?

3 个答案:

答案 0 :(得分:3)

你的GameOverScene不是UIViewController。这样做。

@interface AppDelegate 
{
    ViewController *viewController;
} 
@property (strong, nonatomic) ViewController *viewController;

在ViewController.m中分配。

@implementation ViewController

- (void)viewWillLayoutSubviews
{
     AppDelegate *app = ((AppDelegate *)[[UIApplication sharedApplication] delegate])
     app.viewController = self;
}

//在任何地方

-(void)SetupAdmob  //call only first time
{
       mInterstitial_ = [[GADInterstitial alloc] init];
    mInterstitial_.adUnitID = ADMOB_FULL_SCREEM;
    [mInterstitial_ loadRequest:[GADRequest request]];

    [self performSelector:@selector(showAdmobInterstitial) withObject:nil afterDelay:1.5];
}

-(void)showAdmobInterstitial  //call this to show fullScreen ads
{
    AppDelegate *app = ((AppDelegate *)[[UIApplication sharedApplication] delegate])

    [mInterstitial_ presentFromRootViewController: app.viewController];

    mInterstitial_ = nil;

    mInterstitial_ = [[GADInterstitial alloc] init]; //Cache new ads for next time
    mInterstitial_.adUnitID = ADMOB_FULL_SCREEM;
    [mInterstitial_ loadRequest:[GADRequest request]];

}

答案 1 :(得分:0)

不要忘记设置

@import GoogleMobileAds;
@interface GameViewController : UIViewController <GADBannerViewDelegate, GADInterstitialDelegate>

并设置

self.interstitial.delegate = self;

答案 2 :(得分:0)

Swift中的解决方案更简单,更简单:

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    if (localStorage.on == '1') {
        return {cancel:true};
    }
}, {urls: ["*://*.domain1.net/*","*://*.domain2.com/*","*://*.domain3.com/*"], types: ["script","xmlhttprequest","other"]}, ["blocking"]);
相关问题