2013-04-17 39 views
2

我曾經做過的閱讀周圍公平一點得到了廣告已發現以下幾個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?共享ADBannerView從AppDelegate中 - 如何從委託

我想有一個在didFinishLoading方法的AppDelegate類中加載的單個ADBannerView。希望這個ADBannerView可以被所有相關的ViewController檢索(通過導入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。

我敢肯定我錯過了一些非常基礎的東西。

任何幫助將不勝感激...謝謝,詹姆斯

回答

0

您對您的AppDelegate.m導入ADBannerView.h。 順便問一下,看看蘋果的樣本項目「iAd Suite

0

創建共享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's。

+0

這似乎是在迅速...不objective-c –

相關問題