I'm trying to integrate "admobs" into an scene. I'm trying to do that by doing it viewWillLayoutSubviews in the viewController. I'm trying it out in a blank scene with 0 nodes, so there is nothing that can cause problems in the scene.
The problem is that the ad is not showing in my scene. I'm getting following log message:
To get test ads on this device, call: request.testDevices = #[ GAD_SIMULATOR_ID ];
but i am creating the request in createRequest method.
This is my code:
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
if (!skView.scene) {
self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
self.bannerView.adUnitID = #"ca-app-pub-AdId";
self.bannerView.rootViewController = self;
self.bannerView.delegate = self;
self.bannerView.center = CGPointMake(skView.bounds.size.width / 2, skView.bounds.size.height - (bannerView_.frame.size.height / 2));
[self.view addSubview:self.bannerView];
[self.bannerView loadRequest:[GADRequest request]];
// Create and configure the scene.
SKScene * scene = [Menu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
-(GADRequest *)createRequest {
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
return request;
}
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
NSLog(#"Ad Reveived");
[UIView animateWithDuration:1.0 animations:^{
adView.frame = CGRectMake(0.0, 0.0, adView.frame.size.width, adView.frame.size.height);
}];
}
You need to replace the following code;
[self.bannerView loadRequest:[GADRequest request]];
with:
GADRequest *r = [[GADRequest alloc] init];
r.testing = YES;
[self.bannerView loadRequest:r];
Hope this works for you.
Related
Good day!
I do have a problem with performSegueWithIdentifier: in my game.
There are two view controllers "MainMenu" and "GameViewContoller".
My game starts with MainMenu like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Configure the view.
SKView *skView = (SKView *)self.view;
skView.multipleTouchEnabled = NO;
// Create and configure the scene.
self.scene = [Menu sceneWithSize:skView.bounds.size];
self.scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:self.scene];
UILabel *startButton = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame), 100, 40)];
startButton.text = #"Start";
startButton.font = [UIFont fontWithName:#"out" size:20];
// startButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
startButton.textColor = [UIColor yellowColor];
startButton.userInteractionEnabled = YES;
// startButton.fontSize = 20;
// startButton.name = #"Start";
[self.view addSubview: startButton];
UITapGestureRecognizer *startTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(start)];
[startButton addGestureRecognizer: startTap];
// Do any additional setup after loading the view.
}
-(void) start{
UIViewController *vc = self.view.window.rootViewController;
[vc performSegueWithIdentifier:#"Game" sender:nil];
}
so my game is shutting down when [vc performSegueWithIdentifier:#"Game" sender:nil];
my segues configured correct, there is a segue name "Game". Game is running if is not in test mode from Xcode if so i have an error and second controller is not presented.
Any ideas?
I put an Admob banner at the bottom centre of screen using the following code:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
However, in iPad, I plan to put different ads on another location. Therefore, I need to hide this ads in iPad. Is it possible for me to do this?
try this
bannerView_.hidden=true;
You do not need to make more than one banner view. The easier solution is using the same banner view but checking the current device idiom before positioning the view. For example here is the code you provided modified to check which device the user is on:
- (void) webViewDidFinishLoad: (UIWebView*) webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Change the banner's center/origin here for the iPad.
}
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
My spriteKit game work fine at 60fps but when enabling iAd it drops to 15, sometimes 10 or even lower.. and it's a pain, when I die sometimes it takes a while to reset the scene.
I've ran the analyzer on x-code 6 and I get a lot of low importance warnings:
but also this message:
I've added iAd in the implementation of my viewController.m file like this:
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//AUDIO BACKground
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:#"spaceprueba160kbps" withExtension:#"mp3"];
self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
self.backgroundMusicPlayer.numberOfLoops = -1;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
//iAd
ADBannerView* 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];
// self.canDisplayBannerAds = YES;
// Create and configure the scene.
SKScene * scene = [XYZWorld1 sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
I know I should un-comment that line -> // self.canDisplayBannerAds = YES;
but it work fine like this and if I un-comment this line of code, I get a screen without iAdd and with my main camera looking a bit upper than the main screen.
My game don't run in this file but in an instance of SKScene.
So my question is if anybody know how I can get back my 60 fps while keeping iAd on my screen.. ?
I have the following code in my View Controller:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = NO;
skView.showsNodeCount = NO;
skView.showsDrawCount = NO;
// Create and configure the scene.
SKScene * scene = [MenuScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
}
When I run the application it crashes immediately. I get the following error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView scene]: unrecognized selector sent to instance
I can't use SKView *skView = (SKView *)self.originalContentView; because the app is in landscape mode. Is there a way to display iAds in a Landscap Sprite Kit game?
EDIT:
I just added this code to the view controller, but I get the same results..
#pragma mark iAd Delegate Methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
I ran into this issue and solved it via creating an ABBannerView property and adding that as a subView.
In my ViewController class :
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[self.view addSubview:adView];
Important to NOT set the canDisplayBannerAds property of your view controller.
I believe what is happening is that if you do set the canDisplayBannerAds property to true, the view is modified and is no longer compatible with a SKView, and no longer has a scene property.
I did have to set the frame, so that the dimensions were correct otherwise it was portrait.
You can solve this without having to manually configure the ad banner. You do still have to use the original content view, but making it work correctly is just a matter of putting everything together in the right stages of the view controller's life cycle. All you have to do is enable canDisplayBannerAds: in awakeFromNib. Then set the SKView you create to either the view controller's view or originalContentView depending on the existence of the original content view.
- (void)awakeFromNib
{
[super awakeFromNib];
[self setCanDisplayBannerAds:YES];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
SKView *skView = nil;
if (self.originalContentView) {
skView = (SKView *)self.originalContentView;
}else{
skView = (SKView *)self.view;
}
[skView setShowsDrawCount:YES];
[skView setShowsFPS:YES];
[skView setShowsNodeCount:YES];
SKScene *scene = [MyScene sceneWithSize:skView.bounds.size];
[scene setScaleMode:SKSceneScaleModeFill];
[skView presentScene:scene];
}
None of the above answers works for me. Below code is tested in iOS 7 and 8 and works just fine.
Add below lines in header file
#import <iAd/iAd.h>
#interface GameViewController : UIViewController<ADBannerViewDelegate>{
//iAd
ADBannerView *adView;
}
In Implementation file .m add below code
#import "GameViewController.h"
#import "GameScene.h"
#implementation GameViewController
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
SKView *skView;
if (self.originalContentView) {
skView = (SKView *)self.originalContentView;
}
if (!skView.scene) {
//[skView setShowsDrawCount:YES];
//[skView setShowsFPS:YES];
//[skView setShowsNodeCount:YES];
//Improve Performance
skView.ignoresSiblingOrder = YES;
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
[scene setScaleMode:SKSceneScaleModeFill];
[skView presentScene:scene];
}
}
- (void)awakeFromNib{
[super awakeFromNib];
CGRect screenRect = [[UIScreen mainScreen] bounds];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectMake(0, 0, screenRect.size.width, adView.frame.size.height);
adView.delegate=self;
[self.view addSubview:adView];
}
//iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[adView setAlpha:1.0];
NSLog(#"Show Ad");
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[adView setAlpha:0];
NSLog(#"Hide Ad");
}
//
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
-(BOOL)prefersStatusBarHidden{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#end
i have added all the required files and frameworks for integrating admob. The problem is that it does not add the subview to the viewcontroller.
My viewcontroller consist of a navigation bar and a UITableView. What i want to achieve is to have a admob in the button of my viewcontroller. How can i achieve this.
At the moment i'm using following code, which is not doing anything and not giving me any errors:
bannerView_.adUnitID = #"Banner Key";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
Perhaps you need to set the frame of the banner as well?
1 must set the frame of ad banner so that it can be displayed:
m_bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
m_bannerView.adUnitID = #"AD Unit ID";
m_bannerView.rootViewController = self;
m_bannerView.delegate = self;
m_bannerView.frame = CGRectMake(0, 64, 320, 50); // 64 = height of navigation bar
GADRequest *request = [GADRequest request];
[self.view addSubview:m_bannerView];
[m_bannerView loadRequest:request];
You can set the origin points of the adBanner like below.
self.adBanner = [[GADBannerView alloc]initWithAdSize:kGADAdSizeBanner
origin:CGPointMake(0,31)];
self.adBanner.adUnitID = #"YOUR_KEY";
self.adBanner.delegate = self;
self.adBanner.rootViewController = self;
[self.view addSubview:self.adBanner];
[self.adBanner loadRequest:[self request]];