2011-03-14 75 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