How can I enable iAd when using Sprite Kit? - ios7

In IOS 7, we can enable iAd very easy when using:
self.canDisplayBannerAds = YES;
in code ViewDidLoad of UIViewController
However, I cannot use this in my ViewController (the ViewControl that load SKScene). My game is crash when loading.
So how can I active iAd in my game (using Sprite Kit)?

You can only show iAd in a UIViewController subclass. You are doing it right. Put the following code in your UIViewController and not in the SKScene.
Please refer to this tutorial for the exact code: http://tutorials.veasoftware.com/2013/10/10/how-to-add-iads/

It's possible that you are accessing the view property. When using iAds you need to replace .view with .originalContentView. For example:
SKView* skView = (SKView *)self.view;
is replaced with
SKView* skView = (SKView *)self.originalContentView;

Related

How do I load a .sks linked with .swift class into a UIViewController in my Objective-C project

I have an Objective-C based project in Xcode. I'm trying to add a SpriteKit scene as a smaller view in a view controller. The SpriteKit files are in swift though. I've added Floor1.sks and linked it with Floor1.swift.
Now I'm trying to load it into my Objective-C ViewController.m file. In my storyboard I made the view to the class SKView, and I think I properly made a bridging-header file. My code to insert it at the moment is:
GKScene *scene = [GKScene sceneWithFileNamed:#"Floor1"];
Floor1 *sceneNode = (Floor1 *)scene.rootNode;
sceneNode.scaleMode = SKSceneScaleModeAspectFit;
SKView *skView = (SKView *)_skView;
[skView presentScene:sceneNode];
skView.showsFPS = YES;
skView.showsNodeCount = YES;
//skView loads blank grey screen
The view loads when I run the app successfully, but it's just an empty view with a light grey background with the node count which is 0, and the FPS. What am I doing wrong, how come it won't load my proper view?

Spritekit iAds messing with scene size

I am making a game using spritekit and everything works perfectly except when I add the code to add an iAd. When I add the iAd everything works but it seems that the iAd is resizing the scene when it is being displayed.
This is my code to display the iAd, it is in the ViewController.m :
#import <iAd/iAd.h>
#import "ViewController.h"
#import "MainMenu.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
skView.showsFPS = YES;
// Create and configure the scene.
SKScene * scene = [MainMenu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
Like I say, the iAd shows up, and all the function properly but the when the iAd is displayed it makes the scene smaller, is there a method or something that allows the scenes to not be resized?
An help is much appreciated, thanks!
Ok so I sort of figured it out, I was using scaleMode of SKSceneScaleModeAspectFit I have now changed it to SKSceneScaleModeFill this seems to make the scene shorter but it is not as noticeable as the aspectFit scale mode. I have also noticed that the iAd does not act like a sprite as it actually distorts or resizes the screen. If anyone knows how to create the iAd on top of the view, like sprite, please add a comment or solution.
EDIT
I have just figured out that one can add an ADBannerView to the viewcontroller in the interface builder, this will show the ad on all scenes. I am now trying to figure out how this can be set to not display on specific scenes seeing spritekit only uses one viewcontroller.
If you add the ad by adding in the AdBannerView then you have to create seperate methods in the view controller to turn the ads on or off, by creating these seperate methods it allows one to have manual control over the ads in the view controller from any scene.
In the view controller you have a scene that you create, this scene variable/object has a property of tag or userdata. So if your game goes to a different scene you can call
[super scene] setTag:x]
Every time that the NSTimer calls my control method it checks the value of the scenes tag in the view controller and based on that value it will remove or re-display the ad.
Just a thought, but you could also put the AdBannerView in via the Storyboard editor, and then set
self.canDisplayBannerAds = NO;
That way it will just overlay the SKScene instead of shrinking it.
Hope this helps!
There is another way to fix this problem. You might want to have a look at this Thread.
iAd in Spritekit
Just a sneak preview:
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[adView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:adView];

Integrating a SpriteKit view into a xib view

I have a view that I already created using a xib file.
Now I would like to add some small elements to this view that would make use some of the physics animations from SpriteKit, so now I need an SKView.
Is it possible to add an SKView as a Subview of the view that corresponds to my xib view? I tried this and it does not seem to show anything.
The following is in the ViewController corresponding to my XIB view:
this.myCustomSKView = new CustomSKView()
this.View.AddSubview( this.myCustomSKView );
and the ViewController for my custom SKView has:
public override void ViewWillLayoutSubviews ()
{
base.ViewWillLayoutSubviews ();
if(this.SKView.Scene == null)
{
this.SKView.ShowsFPS = true;
this.SKView.ShowsNodeCount = true;
this.SKView.ShowsDrawCount = true;
var scene = new MyCustomSKScene (this.SKView.Bounds.Size);
this.SKView.PresentScene (scene);
}
}
I just gave this a try, and worked.
Started with single view app.
Dragged in myScene.m / .h from another sprite kit app into my project.
In storyboard dragged in a UIView, and set class to SKView in storyboard.
Created an outlet from that to the VC class (in my case called it myGame)
Added the #import in the VC class
Also copied from the demo project viewDidLoad
This is the only change i made
-(void)viewDidLoad {
[super viewDidLoad];
// Next line is all I changed...
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
I added in some other UIKit to kind show how its a little SK Game in a view.
Not sure if this is the best way but I hope I answers your question.
I agree with lionserdar, and you should check out UIKit Dynamics instead.
I don't know what exactly you are trying to achieve but I think instead of SpriteKit you may wanna check out the UIKitDynamics which provides "physics-related capabilities and animations to views and other dynamic items". I would suggest you look at the apple doc first
https://developer.apple.com/library/ios/samplecode/DynamicsCatalog/Introduction/Intro.html
then
a really nice tutorial on raywenderlich.com
http://www.raywenderlich.com/50197/uikit-dynamics-tutorial
Hope this helps...

MBProgrssHUD error with Cocos2d

Here is the simple code of usage of MBProgressHUD
// Add at the top of the file
#import "MBProgressHUD.h"
// Add right before return TRUE in textFieldShouldReturn
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Redeeming code...";
// Add at start of requestFinished AND requestFailed
[MBProgressHUD hideHUDForView:self.view animated:YES];
And obviously we need a view to add the MBProgressHUD into.
The problem is that there is no view in cocos2d, but only CCNode.
So, is there any way to solve this problem?
By adding an UIview onto a CCLayer ?
If this is a stupid question, please accept my apology as I am still very new in programming.
adding an UIView onto a CCLayer cannot be done because they are separate view hierarchies'
you will need to find your toplevel EAGLView (which subclasses UIView) and add the MBProgressHUD as a child of that. Or use a cocos2d menu system.
Or you could port the MBProgressHUD code into cocos2d?

UIModalPresentationFullScreen not working in iPad landscape mode?

I have added a ViewvController(B) as subview on ViewController(A). In ViewController A(SuperView) UIModelPresentationFullScreen working fine. But when am calling UIModelPresentationFull in ViewController B(SubView) it modelview showing in Portrait mode and that is also not fully viewed. How to solve this problem. Can any one help me please. I have tried 2 days.
This is what I tried in both the superview and subview...
picFBCapture *fbCapt = [[picFBCapture alloc] init];
//[self.navigationController pushViewController:fbCapt animated:YES];
//fbCapt.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
fbCapt.modalTransitionStyle = UIModalPresentationFullScreen;
[self presentModalViewController:fbCapt animated:NO];
[fbCapt release];
Thanks in advance..
The problem is that if you add a view controller as a subview it's not connected to the view controller hierarchy and thus certain things doesn't work. You should avoid adding view controllers as subviews whenever possible, since this is not how Apple intend view controllers to be used, but sometimes it can't be avoided.
If this is one of those cases when it can't be avoided you should save a reference to view controller A in view controller B and then call presentModalViewController: on view controller A (that is connected to the view controller hierarchy) instead of self (view controller B, that isn't connected).
EDIT: In controller A you probably have code looking something like:
[self.view addSubview:controllerB.view];
In conjunction to this line add:
controllerB.controllerA = self;
I hope you know how to create properties, but if not here's a hint:
#property (nonatomic, assign) UIViewController *controllerA;
The rest you should be able to figure out using Google and the documentation.
You will have to handle viewController B's view in landscape by yourself. Since viewController B has been added as a subview, its view controller will not be handling its landscape orientation. The UIModalPresentationFullScreen style (landscape and portrait) will work only if viewController B is shown, ie not as subview but as a full view itself.