I have an iAd which shows up fine when connected to the Network. On the iOS simulator or testing on my device, if I open up my app, see the iAd, then go to settings and turn on airplane mode, and return to the app, the banner slide off the screen. Great. Now, if I turn airplane mode back off (network is ON), the iAd doesn't reappear - even after waiting for 10-15 minutes.
So, here are my questions:
Does the iAd Test Advertisement refresh itself the same as a real iAd would (every minute or so)?
Is there a way to force the iAd to refresh and request a new ad when the network is detected?
I just can't find information on the behaviour of the Test Ads anywhere, and I can't test with real Ads until I upload the app to the App Store (right?)
Heres my code:
Where the iAd is created:
- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 410, 320, 50)];
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];
}
And the delegate methods:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(#"AdWin");
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"AdLose");
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
The delegate method NSLog calls only appear once - not every minute, like I would expect if the Ad was getting refreshed.
1- yes and like live ads there are some times with no ad at all, it can be hours. I believe the refresh cycle is 3 minutes but it seems apple can change it as they will
2- you shouldn't need to, when properly setup this happens automatically. I have an app with ads and it behaves correctly when switching network or using the airplane setting
Now, there seems to be something about the airplane setting, see this thread : iAds Loading Throttled After Re-Launching App From Background (Also Happens In iAdSuite), but I don't have the final answer. Maybe you could explore more by changing your NSLog in didFail... to NSLog(#"%#", error) to check what it exactly says.
Ah and indeed there is no way to test with real ads until it's accepted in the appstore.
Related
I would like to ask the following questions:
1) How to hide iAD when the user clicks on the empty screen?
2) How to identify inactivity i.e. If user has an opened some application and leave the iphone and went away and application remained open?
Update:
According the apple documentation, this method is responsible for dismissing the iAD. but this method is still not working in my code. Any sample or how this method works?
- (void)cancelBannerViewAction
Explanation:
A banner view action can cover your application’s user interface. However, your application continues to run, and receives events normally. If your application receives an event that requires the user’s attention, it can programmatically cancel the action and uncover its interface by calling cancelBannerViewAction. Canceling actions frequently can cause a loss of revenue for your application.
Reference from Apple
But still I am unable to execute? This method is not working properly
You can just do something like below code.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.adBannerViewIsVisible)
{
NSLog(#"\nBanner Success");
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// assumes the banner view is offset 50 pixels so that it is not visible.
banner.frame = CGRectOffset(banner.frame,0,-94);
[UIView commitAnimations];
self.adBannerViewIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.adBannerViewIsVisible)
{
NSLog(#"\nBanner Failed");
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, 94);
[UIView commitAnimations];
self.adBannerViewIsVisible = NO;
}
}
Just specify location of iAd in your code then you can change it's position by just changing value in this line
banner.frame = CGRectOffset(banner.frame, 0, 94);`
Hope this may clear what you want.
I used iAd framework for add ad on screen. I done with this by below code.
but I want ad in UIAertview. When UIAlertview comes user should be view company ad in that.
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatesAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height); [UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animatesAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height); //rect, dx,dy
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I know we can customized UIAlertview. But not getting how to add iAd view in that. If anyone know's help me.
If I'm understanding you correctly, you want to create an ad for your own company? You wouldn't use iAd for this, as with iAd you have very little control over which advertisement is pushed to your banner. However you can make a new view with whatever you want in it (advertisement wise) and add it as a subview of your UIAlertView. Now I am saying you CAN do this, but you probably shouldn't. Modification of an alertView in this way will most likely be rejected by Apple.
I'm testing iAds in XCode 4.
Everything works fine, until the first time a bannerView:didFailToReceiveAdWithError: is received, which I react to by sliding the banner off the screen.
//move the ad back off the screen if an error occurs
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (self.bannerIsInScreenBounds)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// move the banner view off the screen.
banner.frame = CGRectOffset(banner.frame, 320, 0);
[UIView commitAnimations];
}
}
After that, no more bannerViewDidLoadAd: messages are sent to the ADBannerViewDelegate. I'm logging that method right at the top, it's not being called any more.
I'm not releasing the banner or anything, and the ADBannerViewDelegate class is still there and doing other stuff.
What could be wrong?
Thanks.
Why do you expect it load adverts after an error has happened - I think this is the correct behavior.
Looking at the comments it show that the error is "The operation couldn’t be completed. Ad inventory unavailable".
How do you expect it to give you adverts if it can't find any adverts to give ;)
OK, not an ideal solution but here's what I ended up doing.
Whenever I get a didFailToReceiveAdWithError, I wait 10 seconds (to avoid spamming with failures) then recreate the banner.
-(void)replaceAdView {
UIView *adViewSuperview = [adView superview];
[adView removeFromSuperview];
[adView release];
//starting off the screen again
adView = [[NSClassFromString(#"ADBannerView") alloc] initWithFrame:CGRectMake(320, 382, 320, 50)];
adView.delegate = self;
if (adViewSuperview) {
[adViewSuperview addSubview:adView];
}
self.bannerIsInScreenBounds = NO;
}
As my comment at how to implement AdBannerview and ADBannerview delegate, running iphone app in ipad simulator I got didFailToReceiveAdWithError immediately on setting the ADBannerView delegate, and never another delegate call. Running it on the iphone simulator (or changing the app target to universal) the delegate only got called after adding ADBannerView as a subview, and then delegate calls every 30 secs after that.
I have an iAd which displays at the top of a fullscreen subview of the main view. The iAd works normally in portrait mode, and I have handled rotation of the iAd banner view into landscape mode. The issue occurs when the iAd is tapped by the user in landscape mode. The test advertisement displays in portrait, sideways on the phone, and when the user taps the x to dismiss the iAd, the banner view and its parent view are pushed offscreen. The iAd behaves normally in portrait mode (i.e. tapping it and closing it results in the view containing the banner to be displayed normally).
Things I have tried:
- (void)bannerViewActionDidFinish:(ADBannerView *)banner{
NSLog(#"Ad was closed, show the adView again");
if(UIInterfaceOrientationIsLandscape(currentInterfaceOrientation)){
[self animateRotationToLandscape:0.3f];
}
else{
[self animateRotationToPortrait:0.3f];
}
}
-(void)animateRotationToPortrait:(NSTimeInterval)duration{
self.adView.currentContentSizeIdentifier =
ADBannerContentSizeIdentifierPortrait;
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
proUpgradeDescription.frame = CGRectMake(82,313,604,110);
proUpgradePrice.frame = CGRectMake(313,576,142,28);
closeButton.frame = CGRectMake(348,834,72,37);
purchaseButton.frame = CGRectMake(313,431,142,142);
[UIView commitAnimations];
}
else{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
proUpgradeDescription.frame = CGRectMake(20,80,280,70);
proUpgradePrice.frame = CGRectMake(88,322,142,28);
closeButton.frame = CGRectMake(123,403,72,37);
purchaseButton.frame = CGRectMake(88,172,142,142);
[UIView commitAnimations];
}
}
Which calls code that I use to animate rotation of the display for portrait and landscape mode. This code has no effect.
If anyone has any ideas as to why the test advertisements don't rotate correctly and why they push the parent view controller off the screen I would greatly appreciate it.
I don't know if this addresses all of your problems, but according to the answer on this question, the test ads are only in portrait, and real ads will show up in both orientations.
I know the question is a little old so I'm posting here just in case someone runs into the same problem (I did).
ADBannerView messes with the frame and transform properties of the parent view so all you have to do is to reset them to their original values after it has finished (in bannerViewActionDidFinish:).
I still don't understand why it doesn't put back everything the way it was after it has finished. We shouldn't have to do this.
This drove me nuts too. Delivering only landscape full page ads to the iPad and portrait to the iPhone and not saying so is asking for trouble. I gave up using the iAdSuite code, which caused the Landscape iPad ad to leave the screen in Landscape even when the device was in portrait!
This is my code for banner ads. It is all in the first view controller loaded. It aims to put the banner at the bottom of the screen.
In the header file:
#import "iAd/ADBannerView.h"
#property (strong, nonatomic) ADBannerView* adView;
#interface myViewController : UIViewController <ADBannerViewDelegate,
in viewDidLoad
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = CGRectZero;
bannerFrame.size = [adView sizeThatFits:contentFrame.size];
bannerFrame.origin.y = contentFrame.size.height-bannerFrame.size.height;
adView = [[ADBannerView alloc] initWithFrame:bannerFrame];
[adView setDelegate:self];
[self.view addSubview:adView];
Then
-(void)viewWillLayoutSubviews {
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame=CGRectZero;
bannerFrame.size = [adView sizeThatFits:contentFrame.size];
if (adView.bannerLoaded) {bannerFrame.origin.y = contentFrame.size.height-bannerFrame.size.height;}
else {bannerFrame.origin.y = contentFrame.size.height;}
[adView setFrame:bannerFrame];}
Then to handle the callbacks from iAd we need to tell the view to redo its layout if something changes:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];}
This seems to handle the orientation correctly on both iPad and iPhone except for the test full page ads. However, the screen assume correct orientation after the test ad is dismissed so I am hoping it is all OK.
I got a problem to integrate iAd in my iPhone apps -- the banner ad is fine when it expends (see http://www.clingmarks.com/iAd1.png and http://www.clingmarks.com/iAd2.png), however, when I close it, it left a white blank screen (see http://www.clingmarks.com/iAd3.png). I couldn't figure out why. Here is how I integrate the ad:
Because I need to support other ads for lower version of iPhone OSes, I add a container view at the top of the apps, whose view controller is AdViewController. When the view is loaded, I create a AdBannerView programmatically and add it as a subview to the AdViewController.view. Here is the code in the viewDidLoad method:
Class adClass = (NSClassFromString(#"ADBannerView"));
if (adClass != nil) {
iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];
iAdView.frame = CGRectOffset(iAdView.frame, 0, -50);
iAdView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
iAdView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
iAdView.delegate = self;
iadViewIsVisible = NO;
[self.view addSubview:iAdView];
} else {
// init google adsense
}
Following are the delegate methods:
enter code here
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!iadViewIsVisible) {
[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];
iadViewIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (iadViewIsVisible) {
[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];
iadViewIsVisible = NO;
}
}
Eventually I figured it out myself. It turns out the ADBannerView's parent view must be a fullscreen view. I my case above, I added AdBannerView to my adView, which is a view with size 320x50. When I changed its parent view to a fullscreen view, everything works. I am not sure if this is a bug in iAd, but certainly something tricky.
When the banner finishes, it moves itself to the top of the screen even if that means having a negative y coordinate. I center the banner when it finishes. In my case there is a view controller for just the banner, so it is only full screen when the ad is clicked.
-(void) bannerViewActionDidFinish:(UIView *)inBanner {
CGRect frame = [inBanner frame];
frame.origin.x = frame.size.width * 0.5;
frame.origin.y = frame.size.height * 0.5;
[inBanner setCenter:frame.origin];
}
Hey David! I know what you mean, I'm also using an own AdvertisementViewController which calls different ad networks.
So iAd is not in a full screen view but inside a 320x50 view.
Simply do this:
-(void) bannerViewActionDidFinish:(ADBannerView *)inBanner {
[self.view setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)];
}
So the outer view container (self.view) is resized to its original size. iAd is resizing it to fullscreen for displaying the ad when an iAd is shown.