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;
}];
}
Related
I'm getting didFailToReceiveAdWithError message in the console while running on the simulator and device.
iAd banners are displayed successfully when running on iOS 8. When running on iOS 9, iAd banners fail to receive an ad.
.h
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
#property (retain, nonatomic) IBOutlet ADBannerView *adBanner;
.m
-(void)viewDidLoad {
self.adBanner = [[ADBannerView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-100, [UIScreen mainScreen].bounds.size.width, 50)];
self.adBanner.delegate=self;
[self.adBanner setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.adBanner];
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewWillLoadAd");
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Show the ad banner.
NSLog(#"bannerViewDidLoadAd");
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"didFailToReceiveAdWithError");
// Hide the ad banner.
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 0.0;
}];
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(#"Ad did finish");
}
When running on iOS 9, the console prints didFailToReceiveAdWithError every time.
I'm unable to recreate your issue. The iAd network may have been down for your country when testing this, you may be in a country that iAd does not support, or it may be that you've set your iAd Testing Fill Rate to 0% on your development device/simulator. Go to Settings>Developer>Fill Rate> and check that Fill Rate is set to 100% on your development device/simulator.
I'd suggest printing the error you're receiving in didFailToReceiveAdWithError so you can find out why the ADBannerView is failing.
-(void)viewDidLoad {
// The ADBannerView will size itself based on the device it is being displayed on
// Only setting the position is sufficient
self.adBanner = [[ADBannerView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-100, 0, 0)];
self.adBanner.delegate=self;
// Removed setBackgroundColor
// Set alpha to 0.0 initially
self.adBanner.alpha = 0.0;
[self.view addSubview:self.adBanner];
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewWillLoadAd");
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(#"bannerViewDidLoadAd");
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 1.0;
}];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Changed NSLog to print the error that is received
NSLog(#"didFailToReceiveAdWithError: %#", error);
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 0.0;
}];
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
NSLog(#"bannerViewActionDidFinish");
}
If you're still having this issue you should contact iAd directly and update your question based on their response, or post an answer if they're able to solve it for you.
Try adding app transport security in your project's plist file.
I found this here :
Checking my storyboard I noticed, that a height constraint for 32 was set for the ADBannerView - the 32 was not a valid height in that orientation. Removing that height constraint removed the error "Ad inventory unavailable" and it worked beautifully from then on.
Check if that works for you.
Also check with the iAD Changelog to see if there's anything you might need to worry about.
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;
}
}
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
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.
Ok, so after testing on the simulators it turns out that this issue is only occurring on actual devices... How would I fix this?
I am making an iOS app with SpriteKit and am implementing iAD. Everything works more or less like I expect it too, except for one thing. When I tap on the ad, it brings me to a full screen ad, as expected, but when I close that ad the current view freezes, as in nothing happens visually. I know the app is still running because when I click the banner ad again and close out of it again the game returns to normal and the game had progressed while visually frozen. This is how the banner is initialized in my view controller class (the iAD delegate):
self.canDisplayBannerAds = YES;
CGRect bannerFrame = CGRectMake(0, 20, scene.size.width, 50);
banner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
banner.delegate = self;
banner.frame = bannerFrame;
[banner setAlpha:0];
[self.view addSubview:banner];
And these are the loading methods, also in the view controller class:
- (void) bannerViewDidLoadAd:(ADBannerView *) bannerM
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:1];
[UIView commitAnimations];
}
- (void) bannerView:(ADBannerView *)bannerM didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"Ad Failed");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[bannerM setAlpha:0];
[UIView commitAnimations];
}
I don't really understand the problem or why it is happening... Any help would be appreciated!
Thanks,
StrongJoshua
EDIT Here are the two methods that are called when the banner ads open and close:
- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if(playing)
{
NSLog(#"Ad Pause");
SKView *v = (SKView *)self.view;
NSLog(#"2");
SKScene *s = (SKScene *)v.scene;
NSLog(#"3");
WBPlayScene *p = (WBPlayScene *) s;
NSLog(#"4");
[p.logic pause:YES];
NSLog(#"Done");
}
return YES;
}
- (void) bannerViewActionDidFinish:(ADBannerView *)banner
{
if(playing)
{
NSLog(#"Ad Unpause");
[((WBPlayScene *)((SKView *)self.view).scene).logic pause:NO];
}
}
FIXED
The reason for all those NSLogs is because the game crashes when I try to pause it. The game crashes after "2" is reached, so at SKScene *s = (SKScene *)v.scene;. It gives the error [UIView scene]: unrecognized selector sent to instance and I don't understand why...
Solution: To fix this side issue I changed self.view to self.originalContentView and it got the SKView instead of the ad banner's view.
Thanks very much for your help!
SOLVED: I had to remove the call to enable ad display self.canDisplayBannerAds because it interfered with my self-created banner.