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
Related
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];
}
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.
My app has been accepted for iAds advertising network, but after I download the app, I do not see the banner ad appears and I have to wait one week but still not see. I don't know if the app itself is faulty or not, here's the code I've used.
ViewController.h
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
ViewController.m
#pragma mark iAd Delegate Methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
I have implemented the following code:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
And published the app to the app store. Ads are not showing up in the app store app nor am I getting any information in iTunes Connect when I go to view iAd information. Is this code right and the only code I have to put? In the storyboard, I have the iAd at the bottom of the screen connected to the delegate and I also have this in ViewController.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController : UIViewController <ADBannerViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {
(Showing that I have imported the iAd Framework and have the ADBannerViewDelegate)
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];