Sprite Kit 游戏中的 iAd

6
我知道至少有一个问题询问如何将 iAd 集成到 Sprite Kit 游戏中,但这不是我要找的。 我正在寻找一种纵向的方法来完成它。 在网上似乎完全没有关于如何做到这一点的教程,所以我来到了这里。 请问有人可以告诉我如何在 Sprite Kit 游戏中简单地启用 iAd 吗? 我已经启用了 canDisplayBannerAds 并为我的 UIView 使用了 originalContentView 属性,但我一直得到一个崩溃,显示如下:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController originalContentView]: unrecognized selector sent to instance 0x10a0a7410'

任何帮助都将不胜感激,以下是我的视图控制器代码:

- (void)viewWillLayoutSubviews 
{
[super viewWillLayoutSubviews];

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

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

self.canDisplayBannerAds = YES;

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

这不会扭曲或调整你的场景吗? - Maccle415
在某种程度上,是的。如果您想知道如何使其不这样,请访问此处:https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/iAd_Guide/BannerAdvertisements/BannerAdvertisements.html#//apple_ref/doc/uid/TP40009881-CH3-SW2 - user2277872
我所做的就是将iAd视图拖到Storyboard上,然后按照正常方式进行操作。这会将广告放置在所有场景中,因此我必须从每个其他场景访问主场景以更改主场景的userData或标记,然后使用计时器每隔几秒钟检查该值,然后调用一个方法来显示或删除广告。 - Maccle415
我的内容仍然扭曲。你在链接的iAd编程指南中具体做了什么?我对obj-c和iOS都还很陌生。 - HeliNinja
1
我在代码中做了你看到的部分。我已经研究了一些关于正确使用iAd的方法,似乎你应该创建一个ADBannerView *bannerView;类型的变量,在init方法中,你应该将代理设置为self。然后bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 然后在代理方法中,将框架加载到44或类似的东西。只需查看苹果文档,它会给你提供已经给出的代码。 - user2277872
显示剩余3条评论
1个回答

10

对于那些不理解的人和将来需要参考的人,这是我如何将iAd框架添加到我的Sprite Kit游戏中的方法。

我使用Xcode中的SpriteKit模板创建了我的游戏项目,然后进入项目设置:

enter image description here

在“链接二进制文件”部分,只需确保点击+按钮,并添加iAd框架即可。

完成上述步骤后,转到Sprite Kit游戏的视图控制器,然后键入以下内容:

// at the top 
#import <iAd/iAd.h>

// do this in .m, above @implementation
@interface YourViewControllerClassName () 
@property (nonatomic, strong) ADBannerView *banner;
@end

// after the implementation line
// if you're needing to do it horizontally, do:
- (void) viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero];
    self.banner.delegate = self;
    [self.banner sizeToFit];
    self.canDisplayBannerAds = YES;

    SKView *view = (SKView *)self.originalContentView;
    SKScene *scene = [[YourScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];

    [view presentScene:scene];
}

如果您只是在普通的竖屏模式下进行iAd,那么可以使用以上代码,但您也可以只使用- (void) viewDidLoad方法...

现在是使iAd出现的委托方法...

转到@implementation行上面的代码,并进行编辑。

// do this in .m, above @implementation
@interface YourViewControllerClassName () <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *banner;
@end

现在,转到实现行下方,并输入以下内容:

// NOTE: THIS CODE CAME FROM APPLE MOSTLY
// I DID EDIT IT, BUT THE CREDIT GOES TO APPLE'S DOCUMENTATION
// ON IAD 
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{
    if (banner.isBannerLoaded) {
       [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
       // Assumes the banner view is placed at the bottom of the screen.
       banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
       [UIView commitAnimations];
    } 
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{
   if (!banner.isBannerLoaded) {
       [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
       // Assumes the banner view is just off the bottom of the screen.
       banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
       [UIView commitAnimations];
    }
}

这就是让iAd在SpriteKit游戏中实际工作所需的全部内容。我希望我的解释能够帮助那些将要阅读这篇文章的人。


循序渐进地讲解得非常清晰明了。 - chanpkr
谢谢。我搜索了几周,以找到如何在Sprite Kit中使用iAd的方法。我为所有和我一样的人制作了这个。 - user2277872
1
抱歉问这样的问题,但是,如果想把广告放在屏幕顶部而不是底部,应该怎么做呢? - chanpkr
1
我不确定那是否为你想要的,但你只需将 self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; 改成 self.banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 就可以了,不过不要为提问道歉,每个人在使用 iAd 时都会遇到问题.. :) - user2277872
此外,我确实需要在viewWillLayoutSubviews方法中添加[self.view addSubview:self.banner],以便广告能够在我的Sprite Kit场景上实际显示。 - Friendly King

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接