ios - 在 Sprite Kit 场景中隐藏 iAd

标签 ios objective-c sprite-kit

我已使用以下代码将 iAds 添加到我的 Sprite Kit 游戏中:

在viewController.h文件中

@property (strong, nonatomic) IBOutlet ADBannerView * adBannerView;

在viewController.m文件中

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {

        // Create and configure the scene.
        SKScene * scene = [MenuScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;

        _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
        _adBannerView.delegate = self;
        [_adBannerView setFrame:CGRectMake(0, 0, 460, 320)];
        [self.view addSubview:_adBannerView];

        // Present the scene.
        [skView presentScene:scene];
    }
}

这显示了每个场景中的 iAd。有没有办法在某些场景隐藏iAd?

Apple 的 iAd 编程指南说:

Only create a banner view when you intend to display it to the user. Otherwise, it may cycle through ads and deplete the list of available advertising for your application.

这在场景中完全可能吗?

最佳答案

是的,有一种方法可以在某些场景中隐藏 iAd。

- (void)viewDidLoad
{

    [super viewDidLoad];

     //Add view controller as observer
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = NO;
    skView.showsNodeCount = NO;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
    self.canDisplayBannerAds = YES;

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, 0.0f);
    adView.delegate=self;
    [self.view addSubview:adView];

    self.bannerIsVisible=NO;  
}
//Handle Notification
- (void)handleNotification:(NSNotification *)notification
{ 
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    } else if ([notification.name isEqualToString:@"showAd"]) {
        [self showBanner];
    }
}

在你想要隐藏横幅的场景中......

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; 
//Sends message to viewcontroller to show ad.

[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil];  
//Sends message to viewcontroller to hide ad.

关于ios - 在 Sprite Kit 场景中隐藏 iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22078315/

相关文章:

ios - 如何修复此执行但不起作用的代码?

ios - Collision Bitmask Hero 穿过一切,甚至是地面

objective-c - 如何在 xcode 的 NSString 中使用变量作为文件名?

ios - 在 Swift 中将数据从 TableView 传递到选项卡栏 View Controller 。

iphone - 方向更改时的 ModalViewController 尺寸

objective-c - 如何在 Mac OS X 下阻止/更改系统范围的键盘/鼠标事件?

objective-c - UIViewController 子类无法分配实例变量

objective-c - NSWindow 子窗口在父窗口单击时关闭

ios - Xcode 链接器命令失败 - 找不到目录

ios - 为什么 SKSpriteNode.addChild() 不工作