註冊好了,當然要將Admob加到App上,做法可參考Google的文件,原文寫得較完整。
官方原文參考作法 https://developers.google.com/mobile-ads-sdk/docs/
官方中文參考做法 https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals?hl=zh-TW
以下是我的實驗程序。
1. 開一個實驗專案
2. 將googleadmobadssdkios.zip解壓縮後
3. 將.h及.a檔,加到專案裡
4. 然後要加上這個SDK所需要的framework,列表如下
- StoreKit
- AudioToolbox
- MessageUI
- SystemConfiguration
- CoreGraphics
- AdSupport
5.在storyboard上加上測試字樣,以示區別
6. 在mainViewController.h加上Admob元件
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
@interface mainViewController : UIViewController
@property (nonatomic) GADBannerView *bannerView_;
@end
7.在編輯mainViewController.m之前,要先找出發佈商ID(AdMob Publisher ID),在Admob的網站上登入後,在網站與應用程式上找,參考截圖,此例的ID採用另一個App的ID來做測試。
8. 找到ID後,依照參考方法在mainViewController.m加入admob程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize bannerView_;
#define MY_BANNER_UNIT_ID @"a1xxxxxxxxxx" <- 加入App的對應ID
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 在畫面下方建立標準廣告大小的畫面。
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,680.0, 960.0, 120.0)];
// 指定廣告的「單元識別碼」,也就是您的 AdMob 發佈商編號。
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// 指定要復原的 UIViewController,讓執行階段在每次擷取
// 點擊廣告的使用者後加以復原,並加進檢視階層。
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// 啟用泛用請求,並隨廣告一起載入。
[bannerView_ loadRequest:[GADRequest request]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
9. 還要加個Linking參數-ObjC到專案內,請參考圖上位置。
10. 執行後的結果如下,廣告已經出現在畫面的下方了。
11. 這兩天的測試,居然已經有一些廣告收入,只是測試時點了幾下,真不錯。











沒有留言:
張貼留言