iOS - iAd 整合

簡介

IAD由蘋果服務器用於顯示廣告,並幫助我們從應用程序中獲得收入。

涉及的步驟

1. 創建一個簡單的應用程序。

2. 選擇項目文件,然後選擇目標,然後選擇框架加 iAd.framework。

3. 更新 ViewController.h 如下

#import <UIKit/UIKit.h> #import <iAd/iAd.h> @interface ViewController : UIViewController<ADBannerViewDelegate> { ADBannerView *bannerView; } @end

4. 更新 ViewController.m 如下

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; bannerView = [[ADBannerView alloc]initWithFrame: CGRectMake(0, 0, 320, 50)]; // Optional to set background color to clear color [bannerView setBackgroundColor:[UIColor clearColor]]; [self.view addSubview: bannerView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - AdViewDelegates -(void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error{ NSLog(@"Error loading"); } -(void)bannerViewDidLoadAd:(ADBannerView *)banner{ NSLog(@"Ad loaded"); } -(void)bannerViewWillLoadAd:(ADBannerView *)banner{ NSLog(@"Ad will load"); } -(void)bannerViewActionDidFinish:(ADBannerView *)banner{ NSLog(@"Ad did finish"); } @end

輸出

現在,當我們運行程序時,我們會得到下面的輸出。

iOS