Changing iAd position and placement - objective-c

I've been looking at various tutorials and code snippets, try to implement iAd. I've been successful to an extent but I need to customise, I can't seem to work out what I need to do to make my changes, can anyone help?
- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
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.
}
- (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;
}
}
As it stands, the banners are loaded at the top of a UIWebView. Firstly I'd like it at the bottom and secondly, the ad overlaps the WebView content, is there any way I can push the WebView when an ad loads or disappears?
I'd appreciate any help, this is driving round the bend!
Thanks.

set adview.frame.origin.y to however far bottom you want it, when you want it. For instance, my iad is setup in the app delegate, then in the view controller I want, in viewwillapear, I'd use adview.frame.origin.x = 0;adview.frame.origin.y = 420; [SharediAdView setframe:adview.frame];
with diff settings depending on iphone 3.5inch/4inch, space in the current view controller, & orientation.
If you'd rather move everything up/down, and don't want to mess with a tailored solution, I'd try this stuff: https://github.com/chrisjp/CJPAdController

adView.frame = CGRectMake(0.0, 375.0, iAdView.frame.size.width, iAdView.frame.size.height);
Use this code instead of
adView.frame = CGRectOffset(adView.frame, 0, -50);
banner.frame = CGRectOffset(banner.frame, 0, 50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
and change Ad placement (375.0) according to what you want :)

Related

iAd does not work in the other view

I have a little problem with iAd. I used the code shown below. It works well, but only at first View, when I switch to another View, iAd does not show (although I used the exact same code). Do you know where is the problem, please? Thank you for your help.
The code that I used:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
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];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}

WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist

I have a iPad app with 2 view controllers; 1st one is for the UI, the 2nd for a view which displays help from a UIWebView. When I go back and forth 10 times between view controllers, I get the message in the title above on exactly the 10th time returning to the first VC.
This is my code in VC #1:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
[self.view addSubview:adView];
}
- (void) viewWillDisappear:(BOOL)animated {
[_adBanner removeFromSuperview];
_adBanner.delegate = nil;
_adBanner = nil;
}
- (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 know why, just don't know how to fix it. Can someone please help me with this?
You create an instance of ADBannerView in viewDidAppear and assign it to adView You don't appear do anything with this and more relevantly don't discard it in viewWillDisappear so each time you pop back to this view controller the previous instance is orphaned when you cycle through viewDidAppear.
Also possibly another issue is that you are not calling [super viewWillDisappear:] in your viewWillDisappear method.

iAd shows white box

I have a little problem with iAd. Sometimes it shows a blank white box. When I try it with Admob it works perfect. Sometimes the banner comes correctly from the button but sometimes it shows the white box immediately. What is the issue?
Here is my code:
-(void)viewDidLayoutSubviews
{
if (self.view.frame.size.height != self.iAdBannerView.frame.origin.y)
{
self.iAdBannerView.frame = CGRectMake(0.0, self.view.frame.size.height, self.iAdBannerView.frame.size.width, self.iAdBannerView.frame.size.height);
iAdBannerView.requiredContentSizeIdentifiers= [NSSet setWithObjects: ADBannerContentSizeIdentifierLandscape,nil];
iAdBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
if (self.view.frame.size.height != self.gAdBannerView.frame.origin.y)
{
self.gAdBannerView.frame = CGRectMake(150, self.view.frame.size.height, self.gAdBannerView.frame.size.width, self.gAdBannerView.frame.size.height);
}
// Hide the banner by sliding down
-(void)hideBanner:(UIView*)banner
{
if (banner && ![banner isHidden])
{
[UIView beginAnimations:#"hideBanner" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = TRUE;
}
}
// Show the banner by sliding up
-(void)showBanner:(UIView*)banner
{
if (banner && [banner isHidden])
{
[UIView beginAnimations:#"showBanner" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = FALSE;
}
}
#pragma mark - ADBanner delegate methods -
// Called before the add is shown, time to move the view
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
NSLog(#"iAd load");
[self hideBanner:self.gAdBannerView];
[self showBanner:self.iAdBannerView];
}
// Called when an error occured
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"iAd error: %#", error);
[self hideBanner:self.iAdBannerView];
[self.gAdBannerView loadRequest:[GADRequest request]];
}
iAds doesn't always load an ad. There's a delegate method to let you know when an ad loads successfully.
ADBannerViewDelegate has bannerViewDidLoadAd: and bannerView:didFailToReceiveAdWithError:.

iAD Working only on Some Devices

iAd is working fine on iPhone 3GS having iOS 5.0.1 and iPhone 4 having iOS 4.3.1 But it doesn't work on iPad 2 having iOS 5.0.1.
I am Getting the follwoing error.
Error Domain=ADErrorDomain Code=3 "The operation couldn\u2019t be completed. Ad inventory unavailable" UserInfo=0x118ca00 {ADInternalErrorCode=3, NSLocalizedFailureReason=Ad inventory unavailable}
Below is the Code:-
- (void)createAdView {
Class cls = NSClassFromString(#"ADBannerView");
if (cls) {
adView = [[cls alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil];
// Set the current size based on device orientation
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
adView.delegate = self;
adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
// Set intital frame to be offscreen
CGRect adFrame = adView.frame;
adFrame.origin.y = -44;
adView.frame = adFrame;
[UIView beginAnimations:#"showBanner" context:NULL];
[self.view addSubview:adView];
[UIView commitAnimations];
}
}
//Hides iAd Banner
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (bannerIsVisible){
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
adView.frame = CGRectOffset(adView.frame, 0, -44); // Move above screen
[UIView commitAnimations];
bannerIsVisible = NO;
}
}
//Shows iAd Banner.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
adView.frame = CGRectOffset(adView.frame, 0, +44); // Show on top of screen
[UIView commitAnimations];
bannerIsVisible = YES;
}
}
Any help would be appreciated.
The answer is in the error message given: Ad inventory unavailable.
It's not a problem in your code, it's that Apple do not have enough ads to serve. Their fill rate is pretty low so you have to cater for the situation where no ad is available. Many people use services such as AdWhirl to fall back to other ad networks or even display "house" ads.

Why is my iAd banner not showing?

- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
CGRect adFrame = adView.frame;
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
adView.frame = adFrame;
[self.view addSubview:adView];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
self.bannerIsVisible = NO;
}
There are a four things. First, you should be positioning the banner off screen in your viewDidLoad method because it will just show an empty frame when you first launch and will more than likely get rejected because of it.
Secondly, you are setting up your banner view incorrectly. I think the frame is still CGZero. Thirdly you are not setting the bannerView's delegate. Try the following:
-(void)viewDidLoad{
CGRect frame=CGRectZero;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
// Place frame at the bottom edge of the screen out of sight
frame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
// Now to create and configure the banner view
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:frame];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
// Set the delegate to self, so that we are notified of ad responses
adView.delegate = self;
[self.view addSubview: adView];
}
Fourth, in your bannerViewDidLoadAd: method you are not animating the banner ad into place. Try this:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
// Get a brand new frame
CGRect newFrame=CGRectZero;
CGPoint frameOrigin=CGPointZero;
// Set the origin
frameOrigin=CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
newFrame.origin=frameOrigin;
// Set the size
newFrame.size=[ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
CGFloat bannerHeight = newFrame.size.height;
CGFloat bannerOffset=0.0;
// Determine where the new frame should be
if (!self.bannerIsVisible)
{
// It should be visible, raise it up
bannerOffset=-bannerHeight;
}
CGRect offSetRect=CGRectOffset(newFrame,0.0f,bannerOffset);
[UIView animateWithDuration:0.2
animations:^{banner.view.frame=offSetRect}
completion:^(BOOL finished){
if (bannerOffSet<0){
self.bannerIsVisible=YES;
}else{
self.bannerIsVisible=NO;
}
}
];
}
of course if the banner is supposed to be positioned at the top of the screen, you can probably figure out how things need to be modified, but this gets you in going in the right direction.
Good luck
Not sure by reading your question, but do be aware that Apple simulates iAds not being available. Sometimes you need to try multiple times before the sample ad comes through.