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.
Related
I am beginner in iOS. In my ParentViewController I am adding one ChildViewController. In that controller I have added one Next button. When I click that button I am pushing First ChildViewController to Second ChildViewController. That's fine.
But here I have added another Button in Second ChildViewController. When i click that button I want to push back from Second ChildViewController to First ChildViewController. But with my code that's not working. Please help me someone
My code:
ParentViewController:
#import "ParentViewController.h"
#interface ParentViewController ()
#end
#implementation ParentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
ChildViewController1 *ViewController1 = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController1"];
ViewController1.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
ViewController1.view.backgroundColor = [UIColor cyanColor];
[ViewController1 willMoveToParentViewController:self];
[self.view ViewController1.view];
[self ViewController1];
}
#end
ChildViewController1:
#import "ChildViewController1.h"
#interface ChildViewController1 ()
{
}
#end
#implementation ChildViewController1
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)next:(id)sender{
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
middleVC.view.hidden=FALSE;
[middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self addChildViewController:middleVC];
[self.view addSubview:middleVC.view];
[middleVC didMoveToParentViewController:self];
[UIView animateWithDuration:0.5 animations:^{
[middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
ChildViewController2:
#import "ChildViewController2.h"
#interface ChildViewController2 ()
{
}
#end
#implementation ChildViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)back:(id)sender {
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
middleVC.view.hidden=FALSE;
[middleVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self addChildViewController:middleVC];
[self.view addSubview:middleVC.view];
[middleVC didMoveToParentViewController:self];
[UIView animateWithDuration:0.5 animations:^{
[middleVC.view setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
Here when I click this back button it's not pushing it back.
Inside the ChildViewController2 back button action you have:
ChildViewController2 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController2"];
Don't you mean:
ChildViewController1 *middleVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ChildViewController1"];
I want to implement an iAd at the bottom of my screen. My app runs in landscape mode only, but the iAd is in portrait mode, its too big and I want it correct.
I instantiate it with this :
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 480, 32)];
[self.view addSubview:adView];
So i give it 480 x 32 but it is in 360x 50.
That makes no sense to me and I don't know what to do. In the other topic I read I have to disable Auto Layout, but it is disabled.
thanks for any help!
edit: even if I add the iAd via storyboard to the view in landscape format, it shows it in portrait format when i run the app.
edit: I think I don't have a ADBannerView Appdelegate method.
All I have for my iAd is :
GameViewcontroller.h
#interface GameViewController : UIViewController<ADBannerViewDelegate>
GameViewcontroller.m
#interface GameViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width /2) - 180, self.view.frame.size.height, 480, 32)];
_adBanner.delegate = self;
[self.view addSubview:_adBanner];
}
- (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 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 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.
How can I load/unload an iAd when a button is tapped? In other words I want to be able to load a iAd banner when a button is tapped and unload it when another button is tapped.
With the following code I'm able to load and display the banner when the loadBanner button is tapped, the issue starts when I tap the unloadBanner button, it actually unloads the banner but if I click the loadBanner button again it doesn't re-load the banner, which is essentially what I want.
How can i re-load the banner after being removed?
.h file
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
- (IBAction)loadBanner:(id)sender;
- (IBAction)unloadBanner:(id)sender;
#end
.m file
#interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
- (IBAction)loadBanner:(id)sender {
// iADBanner
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 66)];
_adBanner.delegate = self;
}
- (IBAction)unloadBanner:(id)sender {
[_adBanner removeFromSuperview];
_adBanner.delegate = nil;
}
// ===================================
// ********* BANNER ****************
// ===================================
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
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
{
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;
}
}
Thanks a lot