简体   繁体   English

在Sprite Kit场景中隐藏iAd

[英]Hide iAd in Sprite Kit Scene

I have added iAds to my Sprite Kit game with the following code: 我已使用以下代码将iAd添加到我的Sprite Kit游戏中:

In the viewController.h file 在viewController.h文件中

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

In the viewController.m file 在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];
    }
}

This shows the iAd in every scene. 这显示了每个场景中的iAd。 Is there a way to hide the iAd in some of the scenes? 有没有办法在某些场景中隐藏iAd?

Apple's iAd Programming Guide says: 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. 否则,它可能会循环播放广告并耗尽您应用的可用广告列表。

Is this at all possible with scenes? 场景有可能吗?

yes there is a way to hide the iAd in some of the scenes. 是的,有一种方法可以在某些场景中隐藏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];
    }
}

And in your scene in which you want to hide banner... 在你想要隐藏横幅的场景中......

[[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.

Well, In your specific scene follow the Apple's guide(same place as your question) on this issue at the link below, look at the section that says "banner view best practices": https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html#//apple_ref/doc/uid/TP40009881-CH4-SW3 好吧,在您的特定场景中,请在下面的链接中关注此问题的Apple指南(与您的问题相同),查看“banner view best practices”部分: https//developer.apple.com/library/ IOS /文档/ userexperience /概念性/ iAd_Guide / WorkingwithBannerViews / WorkingwithBannerViews.html#// apple_ref / DOC / UID / TP40009881-CH4-SW3

In Summary they say: "remove the banner view from the view hierarchy, set its delegate to nil " 摘要中他们说:“从视图层次结构中删除横幅视图,将其委托设置为nil

The most clean solution is to declare and implement a protocol to let the UIViewController know from the scene that it should hide the ad. 最干净的解决方案是声明并实现一个协议,让UIViewController从场景中知道它应该隐藏广告。

@protocol MySceneDelegate <NSObject>
- (void)hideAd;
@end

@interface MyScene : SKScene
@property (weak) id <MySceneDelegate> delegate;
@end

View controller that shows the scene should implement a hideAd method and set itself as a delegate of the scene. 显示场景的视图控制器应实现hideAd方法并将其自身设置为场景的委托。 Example: 例:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

    // Set the delegate
    [scene setDelegate:self];

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

Then in the scene you can call the hideAd method of the view controller which was set as a delegate: 然后在场景中,您可以调用视图控制器的hideAd方法,该方法被设置为委托:

if ([_delegate respondsToSelector:@selector(closeScene)])
{
    [_delegate performSelector:@selector(hideAd)];
}

And remove the banner in hideAd method. 并在hideAd方法中删除横幅。

To hide the banner view, you should: 要隐藏横幅视图,您应该:

Resize your banner view's frame to be offscreen Resize your content view's frame to cover the space originally hosting the banner 将横幅视图的框架调整为屏幕外框调整内容视图框架的大小以覆盖最初托管横幅的空间

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM