gpt4 book ai didi

iphone - AdBannerView 框架高度始终为零

转载 作者:行者123 更新时间:2023-11-28 23:17:41 25 4
gpt4 key购买 nike

我正在为 iPhone 开发一个需要支持 iAds 的应用程序。该应用程序有一个包含 html 内容的主视图,当加载 iAd 时,它必须调整主视图的大小,以便广告显示在底部。一切都很好并且工作正常,除了以下事实:当我计算主视图和 iAd 横幅的新矩形时,我总是得到 0 作为横幅框架高度。我将 hardcore 50 作为该值,因为我将仅使用纵向定位,但如果有一天 iAd 高度发生变化,我宁愿使用属性方法。这是我完成所有相关工作的类(class)代码(在 __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 并在模拟器上对其进行测试。

最佳答案

也许有帮助

“如果您的应用程序需要在运行时使用广告的确切大小,它会调用 sizeFromBannerContentSizeIdentifier: 类方法,传入 ADBannerContentSizeIdentifierLandscape 或 ADBannerContentSizeIdentifierPortrait。”

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

关于iphone - AdBannerView 框架高度始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5296557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com