【问题标题】:Hide Admob banner in iOS with SpriteKit使用 SpriteKit 在 iOS 中隐藏 Admob 横幅
【发布时间】:2015-04-28 08:38:45
【问题描述】:

我在 Xcode 中使用名为 Sprite kit 的 2d 游戏引擎,我想在游戏场景等特定区域隐藏我的广告横幅,然后在游戏结束后向玩家展示它。但是我在尝试访问其他场景/类中横幅的隐藏属性时遇到了麻烦。

GameViewController.h

#import <UIKit/UIKit.h>

#import <SpriteKit/SpriteKit.h>

#import <GoogleMobileAds/GoogleMobileAds.h>

#import <AVFoundation/AVFoundation.h>

@interface GameViewController : UIViewController

-(void) hideBanner;

@end

GameViewController.m

@implementation GameViewController

-(void) hideBanner {
    self.bannerView.hidden = YES;   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Create a banner ad and add it to the view hierarchy.

    self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];

    //TEST UNIT ID

    self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
    self.bannerView.rootViewController = self;

    [self.view addSubview:self.bannerView];

    GADRequest *request = [GADRequest request];

    request.testDevices = @[ @"*log id*" ];

    [self.bannerView loadRequest:request];
}

GameScene.h

@class GameViewController;

@interface GameScene : SKScene <SKPhysicsContactDelegate>

@property (strong, nonatomic) GameViewController *gameViewController;

@end

GameScene.m

//This line of code will be executed in the "performGameOver" method but it does not work and the banner is still shown? 
    [self.gameViewController hideBanner];

【问题讨论】:

    标签: ios sprite-kit admob


    【解决方案1】:

    你应该使用 NSNotification

    在 viewController.m 中

     - (void)handleNotification:(NSNotification *)notification { 
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    }else if ([notification.name isEqualToString:@"showAd"]) {
        [self showBanner];
    }}
    
    -(void)hidesBanner {
    
        NSLog(@"HIDING BANNER");
        [adView setAlpha:0];
        self.bannerIsVisible = NO;
    }
    
    -(void)showsBanner {
    
        NSLog(@"SHOWING BANNER");
        [adView setAlpha:1];
        self.bannerIsVisible = YES;
    }
    

    在你的场景中:

    向视图控制器发送消息以显示广告。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; 
    

    向视图控制器发送消息以隐藏广告。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; 
    

    更多信息:

    https://stackoverflow.com/a/21967530/4078517

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多