来自 AppDelegate 的共享 ADBannerView - 如何从代表处获取广告
Posted
技术标签:
【中文标题】来自 AppDelegate 的共享 ADBannerView - 如何从代表处获取广告【英文标题】:Shared ADBannerView from AppDelegate - how to get the ad from the delegate 【发布时间】:2013-04-17 20:26:26 【问题描述】:我已经做了很多阅读,发现了以下几个 SO 问题: using shared instance of ADBannerView across app with UITableViews How to make a single shared instance of iAd banner throughout many view controllers? AdBannerView shared across multiple views, including the rootviewcontroller, how?
我想在我的 AppDelegate 类中的 didFinishLoading 方法中加载一个 ADBannerView。希望这个 ADBannerView 可以被所有相关的 ViewControllers 检索(通过导入 AppDelegate.h),然后当用户在 VC 之间移动时相应地显示。
我实现的代码如下:
AppDelegate.h @interface AppDelegate : UIResponder
@property (strong, nonatomic) IBOutlet ADBannerView *adView;
@property (nonatomic, assign) BOOL bannerIsVisible;
- (ADBannerView *)sharedAdBannerView;
AppDelegate.m
ADBannerView *adView;
BOOL bannerIsVisible;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions(NSDictionary *)launchOptions
// create the iAdBannerView
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
adView.delegate=self;
self.bannerIsVisible=NO;
// Override point for customization after application launch.
return YES;
#pragma begin of iAdBannerView Delegate Methods
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
if (self.bannerIsVisible)
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
if (!self.bannerIsVisible)
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
- (ADBannerView *)sharedAdBannerView
return adView;
我也尝试过使用 [NSNotificationCenter defaultCenter] postNotificationName: 方法的变体,但最后我遇到了同样的问题:
问题: 在 ViewController 我想显示 ADBannerView 我如何从 AppDelegate 中检索它?
我目前有:
ADBannerView *banner = [[UIApplication sharedApplication] sharedAdBannerView];
[self.view addSubview:banner];
但是我遇到了很多错误,似乎无法绕过它。
错误:UIApplication 没有可见的@interface 声明扇区 sharedAdbannerView。
我确定我错过了一些非常基本的东西。
任何帮助将不胜感激...谢谢,詹姆斯
【问题讨论】:
【参考方案1】:您必须在您的AppDelegate.m
中导入ADBannerView.h
。
顺便看看苹果示例项目“iAd Suite”
【讨论】:
【参考方案2】:创建共享的iAdBannerView
是这些主题之一,如果您可以阅读数天,最终要么放弃,要么编写必要的几行代码。我选择了第二种方式并创建了这几行代码。
import UIKit
import iAd
// Used thrughout the applcation to show banner when possible
private let applicationADBannerView = ADBannerView(adType: ADAdType.Banner)
// Simple base class for controllers to share one banner view.
// To use this class, inherit a view controller from it and add
// a UIView as container for the banner. The size of the
// container is used as banner size
internal class BaseViewControllerWithBanner: UIViewController, ADBannerViewDelegate
private var bannerContainer: UIView?
internal func viewDidLoad(bannerContainer: UIView)
super.viewDidLoad()
self.bannerContainer = bannerContainer
internal func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!)
log(error)
applicationADBannerView.delegate = nil
applicationADBannerView.removeFromSuperview()
internal override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
applicationADBannerView.delegate = self
applicationADBannerView.frame = bannerContainer?.frame ?? CGRectZero
bannerContainer?.addSubview(applicationADBannerView)
internal override func viewWillDisappear(animated: Bool)
super.viewWillDisappear(animated)
applicationADBannerView.delegate = nil
applicationADBannerView.removeFromSuperview()
要使用这个类,只需继承和覆盖viewDidLoad
方法,如本例所示。
import UIKit
internal class BanneredViewController: BaseViewControllerWithBanner
@IBOutlet weak var bannerViewContainer: UIView! // Position and link this in the xib or storyboard
internal override func viewDidLoad()
log("BanneredViewController: viewDidLoad")
super.viewDidLoad(bannerViewContainer)
... do all other stuff ...
而且它运行良好:所有请求的 100% 都在所有支持 iAd 的国家/地区得到满足。
【讨论】:
这似乎是在 swift... 不是objective-c以上是关于来自 AppDelegate 的共享 ADBannerView - 如何从代表处获取广告的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 AppDelegate 在视图之间共享 iAd 横幅
来自 appDelegate 的 javascript 调用:phonegap iOS
如何在 AppDelegate 和 ViewController 之间共享属性,并在 App 终止前保存
来自 AppDelegate 的 PresentViewController 句柄ActionWithIdentifier
SwiftUI中如何使用@EnvironmentObject在AppDelegate和SceneDelegate/Views之间共享数据