iAD Working only on Some Devices - objective-c

iAd is working fine on iPhone 3GS having iOS 5.0.1 and iPhone 4 having iOS 4.3.1 But it doesn't work on iPad 2 having iOS 5.0.1.
I am Getting the follwoing error.
Error Domain=ADErrorDomain Code=3 "The operation couldn\u2019t be completed. Ad inventory unavailable" UserInfo=0x118ca00 {ADInternalErrorCode=3, NSLocalizedFailureReason=Ad inventory unavailable}
Below is the Code:-
- (void)createAdView {
Class cls = NSClassFromString(#"ADBannerView");
if (cls) {
adView = [[cls alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil];
// Set the current size based on device orientation
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
adView.delegate = self;
adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
// Set intital frame to be offscreen
CGRect adFrame = adView.frame;
adFrame.origin.y = -44;
adView.frame = adFrame;
[UIView beginAnimations:#"showBanner" context:NULL];
[self.view addSubview:adView];
[UIView commitAnimations];
}
}
//Hides iAd Banner
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
adView.frame = CGRectOffset(adView.frame, 0, -44); // Move above screen
[UIView commitAnimations];
bannerIsVisible = NO;
}
}
//Shows iAd Banner.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
adView.frame = CGRectOffset(adView.frame, 0, +44); // Show on top of screen
[UIView commitAnimations];
bannerIsVisible = YES;
}
}
Any help would be appreciated.

The answer is in the error message given: Ad inventory unavailable.
It's not a problem in your code, it's that Apple do not have enough ads to serve. Their fill rate is pretty low so you have to cater for the situation where no ad is available. Many people use services such as AdWhirl to fall back to other ad networks or even display "house" ads.

Related

WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist

I have a iPad app with 2 view controllers; 1st one is for the UI, the 2nd for a view which displays help from a UIWebView. When I go back and forth 10 times between view controllers, I get the message in the title above on exactly the 10th time returning to the first VC.
This is my code in VC #1:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
[self.view addSubview:adView];
}
- (void) viewWillDisappear:(BOOL)animated {
[_adBanner removeFromSuperview];
_adBanner.delegate = nil;
_adBanner = nil;
}
- (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;
}
}
I know why, just don't know how to fix it. Can someone please help me with this?
You create an instance of ADBannerView in viewDidAppear and assign it to adView You don't appear do anything with this and more relevantly don't discard it in viewWillDisappear so each time you pop back to this view controller the previous instance is orphaned when you cycle through viewDidAppear.
Also possibly another issue is that you are not calling [super viewWillDisappear:] in your viewWillDisappear method.

How can I get iAd to fly in to prevent white box?

I am using a shared method to have iAD appear across three view controllers and using the App Delegate. The issue I am having is that when ads do not appear it is forming the white box. I know Apple will not approve the app if the white box appears. What can I code to have the ad start off screen and have it fly in when an ad is present and when an ad is not present, have it fly off.
I assume I would change this on all three views.
Here is the code for each .m
- (AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(void) viewWillAppear:(BOOL)animated{
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;
[_UIiAD setFrame:CGRectMake(100,700,320,50)];
[self.view addSubview:_UIiAD];
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
NSLog(#"ads loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[_UIiAD setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
NSLog(#"ads not loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[_UIiAD setAlpha:0];
[UIView commitAnimations];
}
If it helps, ads are being place on the bottom of a landscape mode iPad
Try this code:
Write this in your .m file:
-(void)AD_init
{
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
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 data");
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
And in your .h file :
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
Answer supplies above. It now works for shared screen iad.

iAd shows white box

I have a little problem with iAd. Sometimes it shows a blank white box. When I try it with Admob it works perfect. Sometimes the banner comes correctly from the button but sometimes it shows the white box immediately. What is the issue?
Here is my code:
-(void)viewDidLayoutSubviews
{
if (self.view.frame.size.height != self.iAdBannerView.frame.origin.y)
{
self.iAdBannerView.frame = CGRectMake(0.0, self.view.frame.size.height, self.iAdBannerView.frame.size.width, self.iAdBannerView.frame.size.height);
iAdBannerView.requiredContentSizeIdentifiers= [NSSet setWithObjects: ADBannerContentSizeIdentifierLandscape,nil];
iAdBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
if (self.view.frame.size.height != self.gAdBannerView.frame.origin.y)
{
self.gAdBannerView.frame = CGRectMake(150, self.view.frame.size.height, self.gAdBannerView.frame.size.width, self.gAdBannerView.frame.size.height);
}
// Hide the banner by sliding down
-(void)hideBanner:(UIView*)banner
{
if (banner && ![banner isHidden])
{
[UIView beginAnimations:#"hideBanner" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = TRUE;
}
}
// Show the banner by sliding up
-(void)showBanner:(UIView*)banner
{
if (banner && [banner isHidden])
{
[UIView beginAnimations:#"showBanner" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = FALSE;
}
}
#pragma mark - ADBanner delegate methods -
// Called before the add is shown, time to move the view
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
NSLog(#"iAd load");
[self hideBanner:self.gAdBannerView];
[self showBanner:self.iAdBannerView];
}
// Called when an error occured
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"iAd error: %#", error);
[self hideBanner:self.iAdBannerView];
[self.gAdBannerView loadRequest:[GADRequest request]];
}
iAds doesn't always load an ad. There's a delegate method to let you know when an ad loads successfully.
ADBannerViewDelegate has bannerViewDidLoadAd: and bannerView:didFailToReceiveAdWithError:.

Admob to load when iAds fail

I just added my first free app to the app store with iAds which work ok when the ads load, so I've had a good read up and it seems a better way is to have admob to load when iads fail.
I can get iads working on there own and admob working on there own but not together any help would be great.
I need to get admob to load when didFailToReceiveAdWithError.
my code for iAds
-(void) bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
my admob code only seems to work in [super viewDidLoad];
here my admob code
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height + 2 - CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin];
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
Place your code in the iad didFailToReceiveAdWithError code like this
-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
//////Admob Code
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height + 2 - CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin];
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}

Changing iAd position and placement

I've been looking at various tutorials and code snippets, try to implement iAd. I've been successful to an extent but I need to customise, I can't seem to work out what I need to do to make my changes, can anyone help?
- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (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
banner.frame = CGRectOffset(banner.frame, 0, 50);
[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;
}
}
As it stands, the banners are loaded at the top of a UIWebView. Firstly I'd like it at the bottom and secondly, the ad overlaps the WebView content, is there any way I can push the WebView when an ad loads or disappears?
I'd appreciate any help, this is driving round the bend!
Thanks.
set adview.frame.origin.y to however far bottom you want it, when you want it. For instance, my iad is setup in the app delegate, then in the view controller I want, in viewwillapear, I'd use adview.frame.origin.x = 0;adview.frame.origin.y = 420; [SharediAdView setframe:adview.frame];
with diff settings depending on iphone 3.5inch/4inch, space in the current view controller, & orientation.
If you'd rather move everything up/down, and don't want to mess with a tailored solution, I'd try this stuff: https://github.com/chrisjp/CJPAdController
adView.frame = CGRectMake(0.0, 375.0, iAdView.frame.size.width, iAdView.frame.size.height);
Use this code instead of
adView.frame = CGRectOffset(adView.frame, 0, -50);
banner.frame = CGRectOffset(banner.frame, 0, 50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
and change Ad placement (375.0) according to what you want :)