I am trying to include iAds in my app. It works fine when there is network connection but the iAds doesn't hide when the network is not available..please find the code below and help me..thanks for your time..
I included this code in viewDidLoad
static NSString * const kADBannerViewClass = #"ADBannerView";
if (NSClassFromString(kADBannerViewClass) != nil) {
if (self.adView == nil) {
self.adView = [[[ADBannerView alloc] init] autorelease];
self.adView.delegate = self;
self.adView.frame = CGRectMake(0,355,320,60);
self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}
}
[self.view addSubview:self.adView];
Delegate methods:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:nil context:NULL];
banner.frame = CGRectOffset(banner.frame, 0,10);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:nil context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -10);
[UIView commitAnimations];
self.bannerIsVisible = NO;
NSLog(#"%#",error);
}
}
If I understand your code correctly, you are initially showing the banner. That is not correct. It is better to initially move the banner off-screen and then only move it on-screen when you receive bannerViewDidLoadAd: and back off-screen when you receive bannerView:didFailToReceiveAdWithError:.
This also has the advantage that your banner view does not initially show up empty. Which can happen if there is a slow network connection.
You can do like this
Hide banner during viewdidload and write this in .m file.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
NSLog(#"Error loading iAd");
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
NSLog(#"Ad loaded");
self.banner.hidden = NO;
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner{
NSLog(#"Ad will load");
self.banner.hidden = NO;
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
NSLog(#"Ad did finish");
self.banner.hidden = NO;
}
Related
I have searched and searched and have done lots of trial and error on this code trying to move the ad banner to the top of the screen. It has been a week already with no progress. If anyone could help me out with this issue I would be thrilled. The code this is based off of is by the gentleman's great tutorial:
http://codewithchris.com/iad-tutorial/
Here's the code:
#interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (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;
}
}
#end
Change this line:
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
To this:
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];
(where the -50 is the height of the ad banner.) Before you were setting the ad banner's frame to the bottom of the screen using self.view.frame.size.height, whereas now the -50 moves the frame just above the screen.
Also, don't forget you'll need to reverse your CGRectOffsets' y coordinates (remove the (-) from the first and add it to the second) because the ones you have at the moment assume the frame is at the bottom of the screen.
Hope this helps!
I have IBOutlet on bottom of the screen for ADBannerView, and I won't to hide it when it shows white screen (not able to load an Ad).
I have this code, but it is not working:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[UIView beginAnimations:#"showAd" context:nil];
CGRect adBannerViewFrame = [bannerView frame];
adBannerViewFrame.origin.x = 160;
adBannerViewFrame.origin.y = 523;
bannerView.frame = adBannerViewFrame;
[UIView commitAnimations];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:#"hideAd" context:nil];
CGRect adBannerViewFrame = [bannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 0;
bannerView.frame = adBannerViewFrame;
[UIView commitAnimations];
}
Can you help me please, I'm struggling with it for past two hours... :/
you should try something like this (this is from a live project). the iAd banner is at the bottom of the screen and it goes down when it becomes invisible, and it comes up back when it must be visible.
the _isiADBannerVisible is just a simple Boolean variable.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (_isiADBannerVisible == false) {
_isiADBannerVisible = true;
[UIView animateWithDuration:0.5f delay:0.f options:UIViewAnimationCurveEaseInOut animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.f, -50.f)];
} completion:nil];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (_isiADBannerVisible == true) {
_isiADBannerVisible = false;
[UIView animateWithDuration:0.5f delay:0.f options:UIViewAnimationCurveEaseInOut animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.f, +50.f)];
} completion:nil];
}
}
I have added iAd to my iphone app to be at the top of my app. originally I place it at x=0 and y=-50 so that it comes from off the screen. I use the following code for it in my .m :
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.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];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[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];
self.bannerIsVisible = YES;
}
}
When my app launch iAd get displayed at the top without any problem. but when I open another app and come back to it (without killing it so my app is running in the background) the banner moves another 50 pixel down
any idea?
You are adding 50.0px to banner.frame.origin.y in both cases.
Anyway: even if you'd be substracting 50.px in didFailToReceiveAdWithError: it
could happen that didFailToReceiveAdWithError: would get called multiple times in a
row and your code could move the banner higher and higher up (-50.0,-100.0,-150.0...).
So it's better to hardcode the hidden & visible positions instead of calculating it.
Try this:
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectMake(0.0,-50.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectMake(0.0,0.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
I'm adding iAd on my app.... I managed to have the banner sliding from the bottom (just above my tab bar, 0, 410 i guess)... but only when i launch the app the first time. when then i try to switch the wifi off on my testing device to check if i get the blank banner i get the following message:ADBannerView: WARNING A banner view (0x1b11d0) has an ad but may be obscured. This message is only printed once per banner view. Can please somebody help me??
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
NSLog(#"there are ads to show");
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
//----hide banner if can't load ad.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
NSLog(#"sorry, no ads ");
banner.frame = CGRectOffset(banner.frame, 0, 410);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
aBanner = [[ADBannerView alloc]initWithFrame:CGRectZero];
aBanner.frame = CGRectOffset(aBanner.frame, 0, 410);
aBanner.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
aBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:aBanner];
aBanner.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
I know something is wrong in the CGRectOffset but I'm not able to figure it out.
thanks
FIND THE PROBLEM.... WAS JUST A SIMPLE "!" HERE THE CODE FIXED:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
NSLog(#"there are ads to show");
banner.frame = CGRectOffset(banner.frame,0, -banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
//----hide banner if can't load ad.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (self.bannerIsVisible) { //THE PROBLEM WAS HERE!!!!
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
NSLog(#"sorry, no ads ");
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
aBanner = [[ADBannerView alloc]initWithFrame:CGRectZero];
aBanner.frame = CGRectOffset(aBanner.frame, 0, 410);
aBanner.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
aBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:aBanner];
aBanner.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
Heres my issue. I have a login screen which is shown using PresentModalViewAnimated. Once the user logins in it is dismissed.
On the login screen i have a iAd but when it is dismissed I get this error.
ADBannerView: WARNING A banner view (0x5a37df0) has an ad but may be
obscured. This message is only printed once per banner view
Also when I login then log back out, the iAd is not displayed again.
Snippets of my code
#synthesize bannerIsVisible;
- (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;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
// stop all interactive processes in the app
// [video pause];
// [audio pause];
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// resume everything you've stopped
// [video resume];
// [audio resume];
}
- (void)viewDidLoad
{
interestingTags = [[NSSet alloc] initWithObjects: INTERESTING_TAG_NAMES2]; //set interestingTag var to define
self.userArray = [[NSMutableArray alloc] init]; //init the userArray
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
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.
}
I think that with this you could to eliminate the error. Just removing the banner view in ViewWillDisappear:
-(void)viewWillDisappear:(BOOL)animated
{
[bannerView removeFromSuperview];
}