简体   繁体   English

iAd精灵套装游戏

[英]iAd in sprite kit game

I know there is at least one question asking how to integrate iAd into a sprite kit game, but that is not what I am looking for. 我知道至少有一个问题询问如何将iAd集成到sprite kit游戏中,但这不是我想要的。 I am looking for a portrait version of how to do it. 我正在寻找一个如何做的肖像版本。 There seem to be absolutely 0 tutorials online as to how to do this, so I came here. 关于如何做到这一点似乎绝对有0个在线教程,所以我来到这里。 Can someone please tell me how to simply enable iAd in a Sprite Kit game? 有人可以告诉我如何在Sprite Kit游戏中简单地启用iAd吗? I have enabled canDisplayBannerAds and have used the originalContentView property for my UIView, but I keep getting a crash that says 我已经启用了canDisplayBannerAds并且已经使用了我的UIView的originalContentView属性,但是我一直在说崩溃

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController originalContentView]: unrecognized selector sent to instance 0x10a0a7410' *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [ViewController originalContentView]:无法识别的选择器发送到实例0x10a0a7410'

any help would be appreciated, and here is my code in my view controller 任何帮助将不胜感激,这是我的视图控制器中的代码

- (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];
}

For those that don't understand, and those that are for future references, here is how I did this to my Sprite Kit game. 对于那些不理解的人,以及那些将来参考的人,这里是我对我的Sprite Kit游戏的做法。

I had my game project created using the SpriteKit template in Xcode, then went to the project settings as so: 我使用Xcode中的SpriteKit模板创建了我的游戏项目,然后进入项目设置:

在此输入图像描述

In the "Link Binary With Libraries" section, just make sure to hit the + button, and add the iAd framework. 在“Link Binary With Libraries”部分中,只需确保点击+按钮,然后添加iAd框架即可。

After doing that, go to your view controller for your Sprite Kit game, and then type this: 完成后,转到您的视图控制器为您的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];
}

If you're doing the iAd in just a normal, portrait, you can do the above code, but you can also just use - (void) viewDidLoad instead... 如果您只是在正常的肖像中进行iAd,您可以执行上面的代码,但您也可以使用 - (void)viewDidLoad代替......

Now is the delegate methods for the iAd to appear... 现在是iAd出现的委托方法......

Go to the code above the @implementation line, and edit it 转到@implementation行上方的代码,然后进行编辑

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

Now, go under the implementation line, and enter this: 现在,进入实现行,输入:

// 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];
    }
}

That is all that is required from the iAd to actually work in a SpriteKit game. 这就是iAd在SpriteKit游戏中实际工作所需的全部内容。 I hope that I helped those that are going to read this. 我希望我能帮助那些读这篇文章的人。

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

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