2016-01-14 56 views
3

這是我第一次整合iAds的體驗。我用下面的鏈接如何以編程方式在iOS中的iAds Banner上展示廣告

http://codewithchris.com/iad-tutorial/

我實現它完美。我的應用程序顯示AddBannerView完美,但廣告隱藏並顯示不起作用。我通過使用storyboard添加了adBannerView,並連接了它的代表和IBOutlets。

-(void)bannerViewDidLoadAd:(ADBannerView *)banner { 
    if (! bannerIsVisible) { 
     // If banner isn't part of view hierarchy, add it 
     if (advertiseView.superview == nil) { 
      [self.view addSubview:advertiseView]; 
     } 

     [UIView beginAnimations:@"animateAdBannerOn" context:NULL]; 

     // Assumes the banner view is just off the bottom of the screen. 
     banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height); 

     [UIView commitAnimations]; 

     bannerIsVisible = YES; 
    } 
} 

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { 
    NSLog(@"Failed to retrieve ad"); 

    if (bannerIsVisible) { 
     [UIView beginAnimations:@"animateAdBannerOff" context:NULL]; 

     // Assumes the banner view is placed at the bottom of the screen. 
     banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height); 

     [UIView commitAnimations]; 

     bannerIsVisible = NO; 
    } 
} 

enter image description here

+1

顯示您使用的代碼 –

+0

該教程相當過時。檢查[這裏 - 斯威夫特](http://stackoverflow.com/a/28639200/2108547)或[here-ObjC](http://stackoverflow.com/a/28708377/2108547)爲更好的實現。 –

+0

究竟是「不工作」?你包含的截圖顯示它實際上工作。請更具體一些。 – jcaron

回答

0

您遵循本教程過時。您的ADBannerView在一段時間內在您的視圖中間結束的原因是因爲您的ADBannerView的委託方法中有您的CGRectOffset。我猜你的問題是你的bannerIsVisibleBOOL

此外,請勿使用beginAnimations:context:方法。從UIView Class Reference

在iOS 4.0及更高版本中不鼓勵使用此方法。您應該使用基於塊的動畫方法 來指定您的動畫。

下面是如何以編程方式實現ADBannerView的示例。此示例使ADBannerViewalpha屬性動起來顯示或隱藏它。沒有必要設置ADBannerView的框架。它會知道它在哪個設備上並適當地調整自己的尺寸。只需設置它的位置就足夠了。

#import "ViewController.h" 
@import iAd; // Import iAd 

@interface ViewController() <ADBannerViewDelegate> { // Include delegate 
    ADBannerView *adView; // Create globally 
} 

@end 

@implementation ViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    adView = [[ADBannerView alloc]init]; // Alloc/init 
    // Position 
    adView.center = CGPointMake(self.view.frame.size.width/2, 
           self.view.frame.size.height - adView.frame.size.height/2); 
    adView.delegate = self; // Set delegate 
    adView.alpha = 0.0; // Hide banner initially 
    [self.view addSubview:adView]; // Add to view 
} 

-(void)bannerViewDidLoadAd:(ADBannerView *)banner { 
    // Delegate method called when the ADBannerView receives an ad 
    NSLog(@"bannerViewDidLoadAd"); 

    // Animate alpha change to show ADBannerView 
    [UIView animateWithDuration:1.0 animations:^{ 
     adView.alpha = 1.0; 
    }]; 
} 

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { 
    // Delegate method called when the ADBannerView fails 
    // Can fail for multiple reasons so lets print why its failing in our NSLog 
    NSLog(@"didFailToReceiveAdWithError: %@", error); 

    // Animate alpha change to hide ADBannerView 
    [UIView animateWithDuration:1.0 animations:^{ 
     adView.alpha = 0.0; 
    }]; 
}