2016年4月14日 星期四

iOS筆記:iAd Banner

  1. 將iAd framework加到專案中:
    在左側的 Project Navigator 中點選Project
    再點選TARGETS中的專案, 標籤選擇 General
    在 Linked Frameworks and Libraries 點選'+'
    尋找iAd.framework 再按 'add'便成功加進專案

enter image description here

  1. 在 ViewController.h 中 import 'iAd.h'檔案並加上 delegate
#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate>
  1. 拖曳或是建立一個Banner View
    (也要將BannerView的Delegate連到ViewController)
    enter image description here

or

@interface ViewController ()
{
    ADBannerView *bannerTestView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    bannerTestView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
    bannerTestView.delegate = self;
    // 預設為隱藏 load成功後再在 delegate 的 method 中顯示
    bannerTestView.alpha = 0.0;
    [self.view addSubview:bannerTestView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    //banner 廣告載入
    NSLog(@"Ad Banner did load ad.");
    // Show the ad banner.
    [UIView animateWithDuration:0.5 animations:^{
        bannerTestView.alpha = 1.0;
    }];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    //使用者點了banner廣告後開啟廣告內容畫面
    return YES;
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    //使用者關掉廣告內容畫面
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    //廣告載入錯誤時會呼叫此方法
}

沒有留言:

張貼留言