iAd banner not working on iOS 9 - ios

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.

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

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.

iAD View Freezes On Ad Close

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.

Is this the right way to supplement AdMob when iAd is unavailable?

I want to fill the iAd space with AdMob advertisements when iAd is unavailable and was wondering if this was the right way to code it to do so. Every time I've run my simulator it has displayed iAd, and when it doesn't it shows a white strip, so I'm wondering if I've coded it wrong. Could someone help me out, and fix the code if necessary, or add suggestions. Thanks.
//iAd Advertising
#pragma mark iAd Delegate Methods
- (void) bannerViewDidLoadAd:(ADBannerView *)banner {
advertisement.hidden = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
//bannerView_.hidden = YES;
} // if there is an internet connection, load the iAd with a 1 second fade in effect
- (void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
advertisement.hidden = YES;
bannerView_.hidden = NO;
bannerView_ = [[GADBannerView alloc]initWithFrame:CGRectMake(0, 20, 320, 50)];
bannerView_.adUnitID = #"//pubname";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
#end
Your code is right. I'm guessing the error is in the storyboard. On the storyboard click the iAd and make sure the Alpha is set to 0. That should solve it.
Sorry for the late answer.

iAd doesn't shows correct

i'm developing dict for iOS, and try to use iAd for monetization. I done step by step with examples, except creating ad view, but ad doesn't show correct.
my code:
- (void)viewDidLoad
{
adView.frame = CGRectOffset(adView.frame, 0, -50); //don't work
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 50); //works well
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (self.bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I work with storyboard, using autolayout, tried to set
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Nothing helped.
Is it showing in the wrong position, or just not showing the ad when built in release mode? If it is in the wrong position, don't use the autoresizingMask and use the autolayout constraints in the storyboard instead. If it is not showing up after building in release/submitting to app store, then you most likely still need to register for the iAd network through iTunesConnect.
It looks like you are using iOS 7. There is a really easy way to add iAds if you don't mind it showing up on the bottom of the view:
#import <iAd/iAd.h>
yourViewController.canDisplayBannerAds = YES;
You do not need to implement any other show/hide methods if you use this property. Apple's WWDC 2013 video titled "iAd Integration and Best Practices" explains this in detail:
https://developer.apple.com/wwdc/videos/

Resources