Just a Computer Graphics Studio & My Life

[iOS] 使用AdMob行動廣告教學

開始要製作免費版的APP,因為是在沒有人贊助的情況下來開發,為了有些許微薄的收入,決定在APP中內嵌入廣告,查詢過網路上有經驗的開發者的文章,發現使用AdMob行動廣告是較有益處!因為它已被Google給買下來⋯⋯此教學是使用AdMob SDK 6.8.0

admob interstitial admob banner

 

AdMob後台可以設定廣告背景文字的顏色,因為我要測試效果,所以就從預設黑色設定為亮粉色囉!

  1. 首先就是要去AdMob申請帳號,接著取得發佈商ID。
  2. 下載好AdMob SDK,將相關檔案匯入我們的專案中。
  3. 加入相關的Framework:
    • AdSupport
    • AudioToolbox
    • AVFoundation
    • CoreGraphics
    • CoreTelephony
    • MessageUI
    • StoreKit
    • SystemConfiguration
  4. 撰寫相關程式碼

/**
 Theme: Using AdMob
 IDE: Xcode 5
 Language: Objective C
 Date: 103/04/04
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
// .h中
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
#import "GADInterstitial.h"

@interface HTAdMobViewController : UIViewController <GADBannerViewDelegate, GADInterstitialDelegate>
{
    GADBannerView *happyBannerView;
    IBOutlet UIView *bannerView;
    GADInterstitial *happyInterstitial;
}
// .m中
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // Create a view of the standard size at the top of the screen.
    // Available AdSize constants are explained in GADAdSize.h.
    happyBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Specify the ad unit ID.
    happyBannerView.adUnitID = @"a15353d0bba1484";

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    happyBannerView.rootViewController = self;
    happyBannerView.delegate = self;

    [bannerView addSubview:happyBannerView];
}

#pragma mark GADRequest generation
- (GADRequest *)request {
    GADRequest *request = [GADRequest request];

    // Make the request for a test ad. Put in an identifier for the simulator as well as any devices
    // you want to receive test ads.
    request.testDevices = @[
                            // TODO: Add your device/simulator test identifiers here. Your device identifier is printed to
                            // the console when the app is launched.
//                            GAD_SIMULATOR_ID
                            ];
    return request;
}

#pragma mark GADBannerViewDelegate implementation
- (void)adViewDidReceiveAd:(GADBannerView *)view {
    NSLog(@"Received ad successfully");
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}

#pragma mark GADInterstitialDelegate implementation
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
    NSLog(@"Received ad successfully");
    [happyInterstitial presentFromRootViewController:self];
}

- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}

-(IBAction)refreshButtonCLicked:(UIButton *)button
{
    // Initiate a generic request to load it with an ad.
    [happyBannerView loadRequest:[self request]];

    happyInterstitial = [[GADInterstitial alloc] init];
    happyInterstitial.delegate = self;
    happyInterstitial.adUnitID = @"a15333bad89ff03";
    [happyInterstitial loadRequest:[self request]];
}

// AppDelegate.m裡寫載入APP時馬上顯示廣告
- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    GADInterstitial *splashInterstitial_ = [[GADInterstitial alloc] init];
    splashInterstitial_.adUnitID = @"a15353d0bba1484";
    [splashInterstitial_ loadAndDisplayRequest:[GADRequest request]
 usingWindow:self.window
 initialImage:[UIImage imageNamed:@“happy_ban.png"]];// 還未載入廣告前的影像
}

之前測試幾乎都會顯示魔神之塔的廣告,四月之後它改到5.0~點擊後去後台查看,可發現數據有在變化,據有經驗人士所述:

  • 行動廣告AdMob(Google):大約0.005~0.03美元每則點擊
  • 網頁廣告AdSense(Google):大約0.17~0.3每則點擊

咱們就來開發許多人會使用的APP,如此使用者一旦量大,我們的收益也跟著多喲!

讀者還沒申請到發佈商ID,我的(見程式碼)可以先拿去測試喔!若本篇文章對你有些許助益,也可以多按廣告贊助,甘恩:)

2015/01/08更新

Admob在2014/09有重大更新,請自行至官網了解喔!

參考:

廣告

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: