iAD size is portrait in landscape - ios

I want to implement an iAd at the bottom of my screen. My app runs in landscape mode only, but the iAd is in portrait mode, its too big and I want it correct.
I instantiate it with this :
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 480, 32)];
[self.view addSubview:adView];
So i give it 480 x 32 but it is in 360x 50.
That makes no sense to me and I don't know what to do. In the other topic I read I have to disable Auto Layout, but it is disabled.
thanks for any help!
edit: even if I add the iAd via storyboard to the view in landscape format, it shows it in portrait format when i run the app.
edit: I think I don't have a ADBannerView Appdelegate method.
All I have for my iAd is :
GameViewcontroller.h
#interface GameViewController : UIViewController<ADBannerViewDelegate>
GameViewcontroller.m
#interface GameViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width /2) - 180, self.view.frame.size.height, 480, 32)];
_adBanner.delegate = self;
[self.view addSubview:_adBanner];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[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];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError: (NSError *)error
{
NSLog(#"Failed to retrieve ad");
if (_bannerIsVisible)
{
[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];
_bannerIsVisible = NO;
}
}

Related

How to ads are displayed on iAds Banner in iOS programmatically

This is my first experience to integrate iAds. I used the following link
http://codewithchris.com/iad-tutorial/
I implemented it perfectly. My application showing AddBannerView perfect but ads hides and showing not working. And I added adBannerView by using storyboard and connect its delegates and IBOutlets.
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (! bannerIsVisible) {
// If banner isn't part of view hierarchy, add it
if (advertiseView.superview == nil) {
[self.view addSubview:advertiseView];
}
[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];
bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"Failed to retrieve ad");
if (bannerIsVisible) {
[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];
bannerIsVisible = NO;
}
}
The tutorial you followed is dated. The reason your ADBannerView is ending up in the middle of your view over a period of time is because of your CGRectOffset in your ADBannerView's delegate methods. I'd guess its a problem with your bannerIsVisible BOOL.
Also, don't use the beginAnimations:context: method. From UIView Class Reference:
Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.
Here's an example of how to implement an ADBannerView programmatically. This example animates the alpha property of the ADBannerView to show or hide it. There's no need to set the ADBannerView's frame either. It will know which device it is on and size itself appropriately. Just setting it's position is sufficient.
#import "ViewController.h"
#import iAd; // Import iAd
#interface ViewController () <ADBannerViewDelegate> { // Include delegate
ADBannerView *adView; // Create globally
}
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
adView = [[ADBannerView alloc]init]; // Alloc/init
// Position
adView.center = CGPointMake(self.view.frame.size.width / 2,
self.view.frame.size.height - adView.frame.size.height / 2);
adView.delegate = self; // Set delegate
adView.alpha = 0.0; // Hide banner initially
[self.view addSubview:adView]; // Add to view
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Delegate method called when the ADBannerView receives an ad
NSLog(#"bannerViewDidLoadAd");
// Animate alpha change to show ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Delegate method called when the ADBannerView fails
// Can fail for multiple reasons so lets print why its failing in our NSLog
NSLog(#"didFailToReceiveAdWithError: %#", error);
// Animate alpha change to hide ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 0.0;
}];
}

Load and Unload iAd view

How can I load/unload an iAd when a button is tapped? In other words I want to be able to load a iAd banner when a button is tapped and unload it when another button is tapped.
With the following code I'm able to load and display the banner when the loadBanner button is tapped, the issue starts when I tap the unloadBanner button, it actually unloads the banner but if I click the loadBanner button again it doesn't re-load the banner, which is essentially what I want.
How can i re-load the banner after being removed?
.h file
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
- (IBAction)loadBanner:(id)sender;
- (IBAction)unloadBanner:(id)sender;
#end
.m file
#interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
- (IBAction)loadBanner:(id)sender {
// iADBanner
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 66)];
_adBanner.delegate = self;
}
- (IBAction)unloadBanner:(id)sender {
[_adBanner removeFromSuperview];
_adBanner.delegate = nil;
}
// ===================================
// ********* BANNER ****************
// ===================================
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[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];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (_bannerIsVisible)
{
[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];
_bannerIsVisible = NO;
}
}
Thanks a lot

iAd banner at bottom of the app for 3.5 and 4 inch screen

Hello I am trying to implement iAds at the bottom of my app When I change size to 3.5" screen, the iAd banner I have at the bottom of screen disappears. When I switch back to 4", the banner is back. How do I lock down the banner to display at bottom of screen regardless of screen size?
Here is my .h file
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
{
ADBannerView *adView;
BOOL bannerIsVisible;
NSInteger HighScoreNumber;
IBOutlet UILabel *HighScore;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
#end
this is .m file
#synthesize bannerIsVisible;
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
//banner is invisible
banner.frame = CGRectOffset(banner.frame, 0, 560);
[UIView commitAnimations];
self.bannerIsVisible = YES;
[banner setAlpha:1];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
//banner is visible and we moved it out of the screen due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -560);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication: (BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction) {
// stop all interactive proccess in the app
// video pause
//audio pause
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// resume everything
// video
// audio
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:#"HighScore: %li", (long)HighScoreNumber];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
- (void) viewWillDisappear:(BOOL)animated
{
[adView removeFromSuperview];
adView.delegate = nil;
adView = nil;
}
Here is all of my code for iAds I really need help with this issue.Thanks
I know this is old but I had this problem recently, and I found a solution so I will post it here in case someone else stumbles upon it.
I noticed that when you change the phones for some reason the ViewController retained the size specified in the storyboard (in my case it was iPhone 6).
So my fix was in the viewDidLoad method I got the size of the main screen after it loaded, instead of the size of the actual view or frame.
Swift:
var screenSize = UIScreen.mainScreen().bounds
println(screenSize)
var viewSize = self.view.bounds
println(viewSize)
Objective-C:
CGSize sizeOfScreen = [[UIScreen mainScreen] bounds].size;
CGSize sizeOfView = self.view.bounds.size;
and the corresponding output in their respective order:
(0.0, 0.0, 320.0, 568.0)
(0.0, 0.0, 375.0, 667.0)
I testing this on a iPhone5S which has a screen size of 320x568, but when asked for the view's bounds I got 375x667 which is the size of which I had made it in the storyboard.
Hope this helps someone!
Add a #define to know which device uses the App :
#define IS_IPHONE5() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && [UIScreen mainScreen].bounds.size.height == 568)
Next, use this #define to modify the offset :
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? 560.0f : 420.0f));
and
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? -560.0f : -420.0f));
Set the right value for iPhone 4" and 3.5"
Set the banner position at viewWillAppear where you can access the self.view.bounds.size, don't forget to add constraints or set autoResizingMask to make it "stick" to the bottom.

2 iAds on simulator

I am trying to create 2 iAds but I have only one on simulator. I can not now check it with device. Why I have only one? I have only second iAd
My code is:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView1.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
adView2 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView2.frame = CGRectOffset(adView2.frame, 0, 200);
adView2.delegate = self;
[self.backgroundView addSubview:adView2];
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
if (banner == adView1)
{
banner.frame = CGRectOffset(banner.frame, 0, 50);
}
if (banner == adView2)
{
banner.frame = CGRectOffset(banner.frame, 0, 200);
}
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
[audio pause];
}
return shouldExecuteAction;
}
(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[audio resume];
}
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
It must be CGRectOffset(adView1.frame, 0, 50); since adView2 is allocation in the next line only???
When you are creating first view, you are using second view's frame:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
As adView2 is not created yet - it is nil and frame is (0,0,0,0)
You have to use adView1 frame for offset as you do for second view.
Also, keep in mind that displaying 2 banners is going against Apple guideline and not recommend. Take a look here (Best practices section)
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html

Hide/Show iAds in Spritekit

I've been trying to figure out how to hide and show iAds in my Spritekit Scenes. Currently I have it setup like this:
ViewController.h
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAD.h>
#interface ViewController : UIViewController <ADBannerViewDelegate> {
ADBannerView *adView;
}
-(void)showsBanner;
-(void)hidesBanner;
#end
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <iAd/iAD.h>
#import "MyScene.h"
#import <SpriteKit/SpriteKit.h>
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 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;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[adView setAlpha:0];
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
-(void)hidesBanner {
NSLog(#"HIDING BANNER");
[adView setAlpha:0];
self.bannerIsVisible = NO;
}
-(void)showsBanner {
NSLog(#"SHOWING BANNER");
[adView setAlpha:1];
self.bannerIsVisible = YES;
}
etc...
#end
Then in my scene I grab my viewcontroller with a pointer:
ViewController *controller;
controller = [[ViewController alloc] init];
[controller hidesBanner];
My nslog runs in the console so I know it's going through. But the banner won't hide. Any thoughts? I'm pretty new with objective c so I have a feeling I'm just doing something dumb.
Like Huygamer said, you're creating a new instance of a view controller so when you call your method [controller hidesBanner]; you're referring to another object.
The best approach here is to use NSNotificationCenter: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
And send a message to your viewcontroller whenever you want to hide or show your ad:
ViewController.m
- (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:
[[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.
Of course, there are 2 object and why you think it can do?
If you want to access the parent of skscene just do this
UIViewController *vc = self.view.window.rootViewController;
You can access the parent of this skscene and you can do hideBanner at the parent of this scene. Simple?
Here is what I did to make it work with SpriteKit Scenes (Xcode 6.1 and iOS 8.1 on iPhone 6):
Step 1- Add #import <"iAd/iAd.h"> in MyScene.h header file
Step 2- Make sure you declare your MyScene class to implement protocol in MyScene.h header file.
Step 3- Add the following code lines in your MyScene.m file inside -(Void)didMoveToView:(SKView *)view function.
ADBannerView* banner=[[ADBannerView alloc]initWithFrame:CGRectZero];
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
banner.frame=bannerFrame;
[self.view addSubview:banner];
banner.delegate=self;
Step 4- Implement the two methods of iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
CGRect bannerFrame =CGRectMake(0, 667-50, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
The above code will move the Ad frame to scene when there is an Ad and will remove the frame if no Ad by animating the movement. Note that the last number in the frame rect is 0. It doesn't matter what you put there, the banner hight is fixed and doesn't change (50 pt).
Step 5- Respond to Ad actions by this code:
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[self startTimer];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
[gameTimer invalidate];
return YES;
}
This code will stop the game timer when a user clicks on the banner and resumes the game timer after the user returns back to the game. You can add your own code for saving and retrieving game data here.
Hope this helps.

Resources