Search code examples
iosxcodeiad

iAd Banner won't load correctly


I followed a youtube tutorial on how to implement shared iads banner in my app. I have 10 VC so this is why i choosed the shared iads via delegate. Whenever I launch the app (On device or simulator) the ad doesn't appear on the first view controller. If i switch to the 2nd VC, it will load and show correctly. Then if i decide to go to the 3rd VC or back to the 1st VC, the banner will be white and no ads will load (even if i wait or go to any other VC). Maybe the tutorial i watched wasn't correctly written, i don't know to be honest. Here's the code used:

AppDelegate.h

@property (strong, nonatomic) ADBannerView *UIiAD;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{     
 _UIiAD = [[ADBannerView alloc] init];
return YES;
}

ViewController.h

@property (strong, nonatomic) ADBannerView *UIiAD;

ViewController.m

- (AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

-(void) viewWillAppear:(BOOL)animated{
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;

if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
    [_UIiAD setFrame:CGRectMake(0,518,320,50)];
}
if ((int)[[UIScreen mainScreen] bounds].size.height == 480)
{
    [_UIiAD setFrame:CGRectMake(0,430,320,50)];
}
if ((int)[[UIScreen mainScreen] bounds].size.height == 667)
{
    [_UIiAD setFrame:CGRectMake(0,600,320,50)];
}
if ((int)[[UIScreen mainScreen] bounds].size.height == 736)
{
    [_UIiAD setFrame:CGRectMake(0,660,320,50)];
}


[self.view addSubview:_UIiAD];
}

-(void) viewWillDisappear:(BOOL)animated{

[_UIiAD removeFromSuperview];
_UIiAD.delegate = nil;
_UIiAD = nil;    
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
NSLog(@"ads loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[_UIiAD setAlpha:1];
[UIView commitAnimations];

self.fullscreen = [[RevMobAds session] fullscreen];
self.fullscreen.delegate = self;
[self.fullscreen loadAd];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
NSLog(@"ads not loaded");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[_UIiAD setAlpha:0];
[UIView commitAnimations];
}

Conclusion

I tried adding a delay in the ViewWillAppear method but it did not work. I placed the banner in the storyboard and made sure it was on 0 alpha. No other answer on SO helped me so hopefully someone will be able to resolve this issue. Thanks!


Solution

  • To conclude my comments,

    1. use storyboard auto layout to position the banner or use a placeholder to get the position of the later banner.
    2. Remove the animations and replace them with .hidden=true and =false.
    3. Set the adBanner to hidden in storyboard or at the point of your initialization.