Shared iAd banner bannerViewDidLoadAd not being called - ios

I am using the following code to setup a shared iAd banner.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_adView = [[ADBannerView alloc]init];
}
ViewController.m
-(void) viewWillAppear:(BOOL)animated {
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
_adView = [appdelegate adView];
_adView.delegate = self;
self.canDisplayBannerAds = true;
}
- (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];
}
bannerView:didFailToReceiveAdWithError is getting called as expected but bannerViewDidLoadAd is never called. I am trying to move some buttons up on the screen when the iAd banner loads.

Your shared banner does not appear to be just one ADBannerView. It looks like you've set multiple #property's for your ADBannerView in your AppDelegate.h and your ViewController.h. Also, self.canDisplayBannerAds = true is creating an entirely new and different ADBannerView for you. self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAds in your application. This will create a ADBannerView for you and show or hide the ADBannerView depending on whether it receives an ad or not from the iAd network. You will want to remove this from your viewDidLoad if you plan to implement a ADBannerView yourself.
Here is what your implementation of your shared ADBannerView should look like:
AppDelegate.h
#import <UIKit/UIKit.h>
#import iAd;
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ADBannerView *iAdView;
#end
AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_iAdView = [[ADBannerView alloc]init];
return YES;
}
ViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface ViewController : UIViewController <ADBannerViewDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
AppDelegate *appDelegate;
}
-(void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
appDelegate.iAdView.delegate = self;
appDelegate.iAdView.frame = CGRectMake(0, 0, appDelegate.iAdView.frame.size.width, appDelegate.iAdView.frame.size.height);
[self.view addSubview:appDelegate.iAdView];
// You created another adView property in your ViewController.h?
//_adView = [appdelegate adView];
//_adView.delegate = self;
// This will actually create ANOTHER ADBannerView
// Do not use when creating your own ADBannerView
//self.canDisplayBannerAds = true;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(#"iAd LOADED");
[UIView beginAnimations:nil context:NULL];
appDelegate.iAdView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(#"iAd FAILED");
[UIView beginAnimations:nil context:NULL];
appDelegate.iAdView.alpha = 0.0;
[UIView commitAnimations];
}

Related

Shared iAd banner unloads ad after dismissing navigation popover

I saw a few other similar questions but could find any answers... I have a shared iAd banner. The problem is when I load it in a modally presented NavigationController with a TableViewController, when I dismiss the TableViewController the Ad Banner in the MainViewController loses the ad and I receive the following error:
ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=7 "The operation couldn’t be completed. Ad was unloaded from this banner" UserInfo=0x5b60e283649 {ADInternalErrorCode=7, NSLocalizedFailureReason=Ad was unloaded from this banner, ADInternalErrorDomain=ADErrorDomain}
The View setup us like this:
MainViewController --> NavigationController --> TableViewController
At another point in the app I modally present a ViewController with shared ad and I don't experience this issue. (MainViewController --> ViewController)
Here is the code from the TableViewController:
.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "AppDelegate.h"
#interface TableViewController : UITableViewController <ADBannerViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *table;
#property (strong, nonatomic) ADBannerView *adView;
#end
.m
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
// Insert Ad Bar
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
_adView = [appdelegate adView];
_adView.delegate = self;
self.canDisplayBannerAds = true;
if (IPAD) {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
}
else {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
}
if (!_adView.bannerLoaded) {
[_adView setAlpha:0];
}
[self.view addSubview:_adView];
}
- (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];
}
Any tips would be appreciated.
Thanks
The problem was because I was using:
self.canDisplayBannerAds = true;
as well as:
if (IPAD) {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
}
else {
[_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
}
if (!_adView.bannerLoaded) {
[_adView setAlpha:0];
}
[self.view addSubview:_adView];
and thus was created 2 instances of the ad bar.

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!
To conclude my comments,
use storyboard auto layout to position the banner or use a placeholder to get the position of the later banner.
Remove the animations and replace them with .hidden=true and =false.
Set the adBanner to hidden in storyboard or at the point of your initialization.

Anything to active iAd?

I have finished my first application. I put iAd banners on it. And active iAd from iTunnes Connect.
I learned to add iAd from this tutorial. http://www.youtube.com/watch?v=fP2ijcXbCz4
Do I need to add something else to my code? Will ads be shown automatically?
#interface TOCGMainViewController () <ADBannerViewDelegate>
#property (strong, nonatomic) IBOutlet ADBannerView *iAdBanner;
#end
#implementation TOCGMainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ([[ UIScreen mainScreen ] bounds ].size.height == 568 ) {
nibNameOrNil = [NSString stringWithFormat:#"%#_568", nibNameOrNil ?: #"TOCGMainViewController"];
}
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
if ([ADBannerView instancesRespondToSelector:#selector(initWithAdType:)]) {
self.iAdBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
self.iAdBanner = [[ADBannerView alloc] init];
}
return self;
}
...
...
...
#pragma mark - ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSUserDefaults *saveApp = [NSUserDefaults standardUserDefaults];
bool saved = [saveApp boolForKey:k_Save];
if (!saved) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
} else {
banner.hidden = YES;
[banner removeFromSuperview];
banner = nil;
banner.delegate = nil;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
#end
To test your iAd code, you can simply run the app on the simulator or your device. If you set up everything correctly, you should see a demo banner that indicates this. This will be replaced with the actual ads when the app goes into the store.

iAd Banner white continue to showing

Adding a iAd banner in my app, but when the banner is empty (white) the banner not become hidden, i try two type of code one is:
on my .h
#import <iAd/iAd.h>
#interface HomeViewController : UIViewController <ADBannerViewDelegate> {
ADBannerView *homeBanner;
}
//----------------------------------iAd BANNER-------------------------//
#property (nonatomic, assign) BOOL bannerIsVisible;
#property (nonatomic, strong) IBOutlet ADBannerView *homeBanner;
#end
on class .m
#synthesize homeBanner, bannerIsVisible;
//------------iAd Banner---------------------------------------//
- (void)bannerViewDidload:(ADBannerView *)abanner {
if (!self.bannerIsVisible){
[UIView beginAnimations:#"animationAdBannerOn" context:NULL];
homeBanner.frame = CGRectOffset(homeBanner.frame, 0.0, 50.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)aBanner {
if (!self.bannerIsVisible){
[UIView beginAnimations:#"animationAdBannerOff" context:NULL];
homeBanner.frame = CGRectOffset(homeBanner.frame, 0.0, -320.0);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
with this code if the banner is white, continue showing.
Try to a second code:
on my .h
#import <iAd/iAd.h>
#interface HomeViewController : UIViewController <ADBannerViewDelegate> {
ADBannerView *homeBanner;
}
//----------------------------------iAd BANNER-------------------------//
#property (nonatomic, strong) IBOutlet ADBannerView *homeBanner;
#end
and on .m
- (void)viewDidLoad {
[super viewDidLoad];
[homeBanner setHidden:YES];
}
- (void)bannerViewDidload:(ADBannerView *)banner {
[homeBanner setHidden:NO];
NSLog(#"Showing");
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[homeBanner setHidden:YES];
NSLog(#"Hidden");
}
and finally have the same problem.
Any idea?
Thanks.
Did you set the delegate for the bannerView?
Try this in you viewDidLoad -
[homeBanner setDelegate:self];

iAd Banner is not working

I'm trying to get a banner in my app, but since I added the banner, the app won't start.
I get an error saying:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named ADBannerView'
Code in .h file:
#import <iAd/iAd.h>
#interface FirstViewController : UIViewController <ADBannerViewDelegate>
{
ADBannerView *banner;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
#property (nonatomic,retain) IBOutlet ADBannerView *banner;
Code in .m file:
#synthesize banner, bannerIsVisible;
-(void)bannerViewDidLoad: (ADBannerView *)abanner
{
if(!self.bannerIsVisible)
{
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
banner.frame=CGRectOffset(banner.frame, 0.0, 50.0);
[UIView commitAnimations];
self.bannerIsVisible=YES;
}
}
-(void)bannerView:(ADBannerView *)aBanner
{
if(!self.bannerIsVisible)
{
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
banner.frame=CGRectOffset(banner.frame, 0.0, -320.0);
[UIView commitAnimations];
self.bannerIsVisible=NO;
}
}
What do you think is wrong?
You must add iAd.framework into your project.
Take this code:
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate> {
}
#end
.m file:
#implementation ViewController
-(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];
}
#end

Resources