Reputation: 590
I'm developing an App for iPhone which needs to have support for iAds. The App has a main view with html content and when an iAd is loaded it has to resize the main view so the Ad is shown at the bottom. Everything is fine and working except for the fact that, when I do the maths to calculate the new rectangles of both the main view and the iAd banner, I always get 0 as the banner frame height. I cold hardcore 50 as that value since I'll use only portrait positioning but I'd rather use a property approach if some day the iAd height changes.
Here's the code of the class where I do all the related work (math done in __show
method):
//
// SAiOSAdPlugin.m
// Ad Plugin for PhoneGap
//
// Created by shazron on 10-07-12.
// Copyright 2010 Shazron Abdullah. All rights reserved.
//
#import "SAiOSAdPlugin.h"
@interface SAiOSAdPlugin(PrivateMethods)
- (void) __prepare:(BOOL)atBottom;
- (void) __showAd:(BOOL)show;
@end
@implementation SAiOSAdPlugin
@synthesize adView;
@synthesize bannerIsVisible, bannerIsInitialized, bannerIsAtBottom;
const int AdHeight = 50;
#pragma mark -
#pragma mark Public Methods
- (void) prepare:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSUInteger argc = [arguments count];
if (argc > 1) {
return;
}
NSString* atBottomValue = [arguments objectAtIndex:0];
[self __prepare:[atBottomValue boolValue]];
}
- (void) showAd:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSUInteger argc = [arguments count];
if (argc > 1) {
return;
}
NSString* showValue = [arguments objectAtIndex:0];
[self __showAd:[showValue boolValue]];
}
#pragma mark -
#pragma mark Private Methods
- (void) __prepare:(BOOL)atBottom
{
NSLog(@"SAiOSAdPlugin Prepare Ad At Bottom: %d", atBottom);
Class adBannerViewClass = NSClassFromString(@"ADBannerView");
if (adBannerViewClass && !self.adView)
{
self.adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
self.adView.requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
self.adView.delegate = self;
}
if (atBottom)
{
self.bannerIsAtBottom = YES;
}
self.bannerIsVisible = NO;
self.bannerIsInitialized = YES;
}
- (void) __showAd:(BOOL)show
{
NSLog(@"SAiOSAdPlugin Show Ad: %d", show);
if (!self.bannerIsInitialized){
[self __prepare:NO];
}
if (!(NSClassFromString(@"ADBannerView") && self.adView)) { // ad classes not available
return;
}
if (show == self.bannerIsVisible) { // same state, nothing to do
return;
}
CGRect adViewFrame = self.adView.frame;
CGRect webViewFrame = [super webView].frame;
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
if (self.bannerIsAtBottom)
{
CGRect adViewFrame = self.adView.frame;
printf("AdView Show: StatusBarHeight: %f, adViewFrameHeight: %f\n", statusBarHeight, adViewFrame.size.height);
adViewFrame.origin.y = [UIScreen mainScreen].bounds.size.height - statusBarHeight - adViewFrame.size.height;
printf("AdView origin Y: %f\n", adViewFrame.origin.y);
self.adView.frame = adViewFrame;
}
if (show)
{
if (self.bannerIsAtBottom)
{
webViewFrame.size.height -= (adViewFrame.size.height + statusBarHeight);
}
else
{
webViewFrame.origin.y += adViewFrame.size.height;
webViewFrame.size.height -= (adViewFrame.size.height + statusBarHeight);
}
[UIView beginAnimations:@"blah" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[super webView].frame = webViewFrame;
[[[super webView] superview] addSubview:self.adView];
printf("AdView on show: %f, %f\n", self.adView.frame.origin.x, self.adView.frame.origin.y);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
else
{
if (self.bannerIsAtBottom)
{
webViewFrame.size.height += (adViewFrame.size.height + statusBarHeight);
}
else
{
webViewFrame.origin.y -= adViewFrame.size.height;
webViewFrame.size.height += (adViewFrame.size.height + statusBarHeight);
}
[UIView beginAnimations:@"blah" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[super webView].frame = webViewFrame;
[self.adView removeFromSuperview];
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
#pragma mark -
#pragma ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
Class adBannerViewClass = NSClassFromString(@"ADBannerView");
if (adBannerViewClass)
{
NSString* jsString =
@"var e = document.createEvent('Events');"
"e.initEvent('iAdBannerViewDidLoadAdEvent');"
"document.dispatchEvent(e);";
[super writeJavascript:jsString];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
Class adBannerViewClass = NSClassFromString(@"ADBannerView");
if (adBannerViewClass)
{
NSString* jsString =
@"var e = document.createEvent('Events');"
"e.initEvent('didFailToReceiveAdWithError');"
"document.dispatchEvent(e);";
[super writeJavascript:jsString];
}
}
@end
For the record I'm using the iOS 4.3 SDK and testing it on the emulator.
Upvotes: 0
Views: 1301
Reputation: 3303
maybe it helps
"If your application needs the exact size of the advertisement to use at runtime, it calls the sizeFromBannerContentSizeIdentifier: class method, passing in either ADBannerContentSizeIdentifierLandscape or ADBannerContentSizeIdentifierPortrait."
Upvotes: 2