2011-03-14 126 views
0

我正在开发应用程序iPhone需要支持iAds。该应用程序具有html内容的主视图,并且当加载iAd时,它必须调整主视图的大小,以便广告显示在底部。一切都很好,除了当我做数学计算主视图和iAd横幅的新矩形时,我总是得到0作为横幅框架高度。我冷硬核心50作为价值,因为我将只使用肖像定位,但我宁愿使用属性方法,如果有一天iAd高度变化。 这里就是我做的所有相关工作(数学中__show方法进行)之类的代码:AdBannerView框架高度始终为零

// 
// 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,并在模拟器上测试它。

回答

2

也许它可以帮助

“如果你的应用需要广告的确切大小在运行时使用,它调用sizeFromBannerContentSizeIdentifier:类方法,通过在任一ADBannerContentSizeIdentifierLandscape或ADBannerContentSizeIdentifierPortrait。”

源 - http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/iAd_Guide/BannerAdvertisements/BannerAdvertisements.html#//apple_ref/doc/uid/TP40009881-CH3-SW2

+0

呀看来我的片段是较早版本的SDK,现在的规模已经被恢复的方式。谢谢! – brafales 2011-03-16 13:48:27