我正在为 iPhone 开发一款需要支持 iAds 的应用。该应用程序具有html内容的主视图,当加载iAd时,它必须调整主视图的大小,以便广告显示在底部。一切都很好并且工作正常,除了当我做数学计算主视图和iAd横幅的新矩形时,我总是得到0作为横幅框架高度。因为我只使用肖像定位,所以我将冷硬核50作为该值,但是如果有一天iAd高度发生变化,我宁愿使用属性方法。
这是我完成所有相关工作的类的代码(用__show
方法完成的数学运算):
//
// 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
对于记录我正在使用iOS 4.3 SDK并在模拟器上进行测试。
答案 0 :(得分:2)
也许有帮助
“如果您的应用程序需要在运行时使用的广告大小,它会调用sizeFromBannerContentSizeIdentifier:class方法,传入ADBannerContentSizeIdentifierLandscape或ADBannerContentSizeIdentifierPortrait。”