2015-06-06 134 views
1

我正在使用以下代碼來設置共享的iAd橫幅。共享iAd橫幅bannerViewDidLoadAd未被調用

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    _adView = [[ADBannerView alloc]init]; 
} 

ViewController.m

-(void) viewWillAppear:(BOOL)animated { 
    AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate]; 
    _adView = [appdelegate adView]; 
    _adView.delegate = self; 
    self.canDisplayBannerAds = true; 
} 

- (void)bannerViewDidLoadAd:(ADBannerView *)banner { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1]; 
    [banner setAlpha:1]; 
    [UIView commitAnimations]; 
} 

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1]; 
    [banner setAlpha:0]; 
    [UIView commitAnimations]; 
} 

bannerView:didFailToReceiveAdWithError如預期,但bannerViewDidLoadAd不會被調用獲取調用。我試圖在iAd旗幟加載時在屏幕上移動一些按鈕。

回答

1

您的共享橫幅看起來不是一個ADBannerView。看起來您已在您的AppDelegate.h和您的ViewController.h中爲您的ADBannerView設置了多個@property。另外,self.canDisplayBannerAds = true正在爲您創建完整的新的和不同的ADBannerViewself.canDisplayBannerAds = true可以用於沒有麻煩在您的應用程序中實現iAds的方式。這將爲您創建一個ADBannerView,並根據它是否從iAd網絡收到廣告來顯示或隱藏ADBannerView。如果您打算自己實施ADBannerView,則需要從viewDidLoad中刪除此項。

這裏是你的執行你的分享ADBannerView應該是這樣的:

AppDelegate.h

#import <UIKit/UIKit.h> 
@import iAd; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> { 

} 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) ADBannerView *iAdView; 

@end 

AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    _iAdView = [[ADBannerView alloc]init]; 
    return YES; 
} 

ViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 

@interface ViewController : UIViewController <ADBannerViewDelegate> 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController () 

@end 

@implementation ViewController { 
    AppDelegate *appDelegate; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate]; 
    appDelegate.iAdView.delegate = self; 
    appDelegate.iAdView.frame = CGRectMake(0, 0, appDelegate.iAdView.frame.size.width, appDelegate.iAdView.frame.size.height); 
    [self.view addSubview:appDelegate.iAdView]; 

    // You created another adView property in your ViewController.h? 
    //_adView = [appdelegate adView]; 
    //_adView.delegate = self; 

    // This will actually create ANOTHER ADBannerView 
    // Do not use when creating your own ADBannerView 
    //self.canDisplayBannerAds = true; 
} 

-(void)bannerViewDidLoadAd:(ADBannerView *)banner { 
    NSLog(@"iAd LOADED"); 
    [UIView beginAnimations:nil context:NULL]; 
    appDelegate.iAdView.alpha = 1.0; 
    [UIView commitAnimations]; 
} 

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { 
    NSLog(@"iAd FAILED"); 
    [UIView beginAnimations:nil context:NULL]; 
    appDelegate.iAdView.alpha = 0.0; 
    [UIView commitAnimations]; 
} 
+0

感謝丹尼爾。如果我使用self.canDisplayBannerAds = true;並消除其他橫幅廣告,它仍然會調用bannerViewDidLoadAd:? –

+0

@JasonBestor簡單地說,沒有。你可以用更多的代碼來實現這個目標,但是如果你打算這麼做的話,你可以像上面所展示的那樣實現iAd橫幅。 –

+0

@DanialStorm我問,因爲我的應用程序有幾個其他視圖,其中使用self.canDisplayBannerAds = true;會在廣告出現時自動向上移動桌面視圖等等。此外,當我導航回主視圖時,橫幅視圖正在消失。 –