我已经在
swift中复制了
iAdSuite with Storyboards,并且iAd在所有视图控制器上都可以正常工作,除了当调用委托方法bannerViewDidLoadAd(banner:ADBannerView!)时,横幅视图不会立即出现.我必须旋转模拟器或转到另一个标签,以便它出现.
我认为这是Objective-C中Apple示例代码的一部分,我无法“翻译”为swift?这可能导致iAd一旦加载就不会异步显示…
+ (BannerViewManager *)sharedInstance { static BannerViewManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken,^{ sharedInstance = [[BannerViewManager alloc] init]; }); return sharedInstance; }
这是我的BannerViewManager类,带有ADBannerViewDelegate方法.
class BannerViewManager: NSObject,ADBannerViewDelegate { // MARK: - Shared instance static let sharedInstance = BannerViewManager() var bannerView: ADBannerView! var bannerViewControllers: NSMutableSet! override init() { super.init() bannerView = ADBannerView(adType: .Banner) bannerViewControllers = NSMutableSet() bannerView.delegate = self } func addBannerViewController(controller: BannerViewController) { bannerViewControllers.addObject(controller) } func removeBannerViewController(controller: BannerViewController) { bannerViewControllers.removeObject(controller) } // MARK: - Ad banner view delegate methods func bannerViewDidLoadAd(banner: ADBannerView!) { NSLog("bannerViewDidLoadAd") for bannerViewController in bannerViewControllers { (bannerViewController as! BannerViewController).updateLayout() } } func bannerView(banner: ADBannerView!,didFailToReceiveAdWithError error: NSError!) { NSLog("didFailToReceiveAdWithError %@",error); for bannerViewController in bannerViewControllers { (bannerViewController as! BannerViewController).updateLayout() } } func bannerViewActionShouldBegin(banner: ADBannerView!,willLeaveApplication willLeave: Bool) -> Bool { NSNotificationCenter.defaultCenter().postNotificationName(BannerViewActionWillBegin,object: self) return true } func bannerViewActionDidFinish(banner: ADBannerView!) { NSNotificationCenter.defaultCenter().postNotificationName(BannerViewActionDidFinish,object: self) } }
编辑
我已设法使用this post on SO将上面的Objective-C代码转换为swift但我仍然遇到同样的问题. iAd一旦加载就不会出现.必须对视图进行某种更改才能实现,无论是旋转设备还是点击另一个选项卡等等……
所以现在我的sharedInstance看起来像这样:
static var sharedInstance: BannerViewManager { struct Static { static var onceToken: dispatch_once_t = 0 static var instance: BannerViewManager? = nil } dispatch_once(&Static.onceToken) { Static.instance = BannerViewManager() } return Static.instance! }