Porto 中介服務 AdMob廣告

Document

若需在 iOS 應用程式中呈現 ClickForce 廣告,只要在您的 Xcode 專案中導入 SDK,然後在使用者介面中加入相關指令就行了。

SDK導入說明

須先行下載頁面上的AdMob中介服務分類好的SDK檔案後,即進行解壓縮程序,Banner、Interstitial及Native 三種類型目錄夾內有必須使用的自定義Class檔案及一個iMFAD Framework及一個GoogleMobileAds framework,並完成以下三項前置步驟:


A.在本專案中加入 iMFAD Framework

B.設定廣告形式所需要檔案

C.在 Build Settings 內 Other Linker Flags 請填入 -all_load 與 -ObjC

基本設定


iMFAD Framework 加入方式

Step1:

iMFAD.framework 拖曳到 XCode 中您的應用程式目錄內。選擇「Copy Items if needed」(複製必要項目),然後點擊「Finish」(完成)



Step2:

在TARGETS-開啟 Build Phases 分頁下的 Link Binary With Libraries 下拉式選單,
然後用畫面上出現的 +按鈕加入 剛剛拖拉至專案中的iMFAD.framework




GoogleMobileAdsSdk 設定


修改Build Settings

在專案設定選項中的 Build Settings下的 Other Linker Flags 加入-ObjC-all_load



修改info.plist


iOS9 App Transport Security Settings


1.加入步驟為 新增App Transport Security Settings , 並在底下新增Allow Arbitrary Loads ,將其設為 YES


AdMob GADIsAdManagerApp


1.加入步驟為 新增GADIsAdManagerApp ,Type 選擇 Boolean,將其設為 YES


AdMob 設定

前往AdMob網站設定應用程式。新增完應用程式後,並記下你的廣告單元編號再從中介服務新增廣告來源,如下圖:

首先建立廣告單元:


選擇廣告單元廣告格式:


編輯廣告單元名稱:


建立完成廣告單元:


建立中介服務群:


建立廣告格式跟平台:


建立地區跟建立廣告單元:


廣告單元設置:


新增自訂事件:


自訂事件設定:


設定自訂廣告單元: (Class Name and Parameter)



完成:


ClickForce AD 設定


宣告及引入

1.依照在AdMob平台上所設定的Class 名稱,將該同名Class引入至專案。 (該Class在IOS-AdMob-Banner壓縮包中)


2.加入程式碼


請在需要引入廣告的地方加入@import GoogleMobileAds ,並加入GADBannerViewDelegate

#import <UIKit/UIKit.h>


@import GoogleMobileAds;

@interface ViewController : UIViewController<GADBannerViewDelegate>
{
	
}

@end

以下程式碼會在 viewController的 viewDidLoad 初始化步驟中建立橫幅廣告(Banner)。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //Mediation
    GADBannerView  *bannerView = [[GADBannerView alloc]
                                  initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(320, 50))
                                  origin:CGPointMake((self.view.frame.size.width-320)/2, self.view.frame.size.height-50)];
    
    [self.view addSubview:bannerView];
    
    bannerView.adUnitID = @"ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX";
    bannerView.rootViewController = self;
    bannerView.delegate = self;
    GADRequest *request = [GADRequest request];
    [bannerView loadRequest:request];
}


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


@end

取得結果



宣告及引入

1.依照在AdMob平台上所設定的Class 名稱,將該同名Class引入至專案。 (該Class在IOS-AdMob-Interstitial壓縮包中)


2.加入程式碼


請在需要引入廣告的地方加入@import GoogleMobileAds ,並加入GADInterstitialDelegate

#import <UIKit/UIKit.h>
@import GoogleMobileAds;

@interface ViewController : UIViewController<GADInterstitialDelegate>
{

}
@end

以下程式碼會在 viewController的 viewDidLoad 初始化步驟中建立插頁廣告(Interstitial)。

#import "ViewController.h"

@interface ViewController ()
{
    GADInterstitial *intertitial;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //Mediation
    intertitial = [[GADInterstitial alloc]initWithAdUnitID:@"ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX"];
    intertitial.delegate = self;
    GADRequest *request = [GADRequest request];
    [intertitial loadRequest:request];
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    [intertitial presentFromRootViewController:self];
}


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


@end

取得結果



宣告及引入

1.依照在AdMob平台上所設定的Class 名稱,將該同名Class引入至專案。 (該Class在IOS-AdMob-Native壓縮包中)


2.加入程式碼


請在需要引入廣告的地方加入@import GoogleMobileAds ,並加入GADUnifiedNativeAdDelegate、GADUnifiedNativeAdLoaderDelegate、GADMediationAd

#import <UIKit/UIKit.h>
@import GoogleMobileAds;

@interface ViewController : UIViewController<GADNativeAdLoaderDelegate, GADNativeAdDelegate>
{
    GADAdLoader *adLoader;
    GADNativeAdView *nativeAdview;
    UIImageView *iconView;
    UILabel *Adtittle;
    UILabel *Advertiser;
    UILabel *Adbody;
    GADMediaView *mediaView;
    UIButton *Adbutton;
    NSLayoutConstraint *heightConstraint;
}
@end

以下程式碼會在 viewController的 viewDidLoad 初始化步驟中建立原生廣告(Native))。

#import "ViewController.h"
// Native Advanced ad unit ID for testing.
static NSString *const ClickforceTestAdUnit = @"ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX";
@interface ViewController ()
{
	
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [nativeAdview removeFromSuperview];
    [self setAdview];
    
    GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
    videoOptions.startMuted = YES;
    
    adLoader = [[GADAdLoader alloc] initWithAdUnitID:ClickforceTestAdUnit
                                  rootViewController:self
                                             adTypes:@[kGADAdLoaderAdTypeUnifiedNative]
                                             options:@[videoOptions]];
    
    adLoader.delegate = self;
    GADRequest*req = [GADRequest request];
//    req.testDevices = @[@""];//set your Test deivce id.
//    req.testDevices = @[kGADSimulatorID];//set Test Simulators ID
    [adLoader loadRequest:req];
}

- (void) setAdview{
    
    nativeAdview = [[GADNativeAdView alloc] initWithFrame:CGRectMake(0, 50, 359, 300)];
    
    iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    Adtittle = [[UILabel alloc] initWithFrame:CGRectMake(57, 15, 285, 20)];
    Advertiser = [[UILabel alloc] initWithFrame:CGRectMake(57, 38, 285, 17)];
    Adbody = [[UILabel alloc] initWithFrame:CGRectMake(15, 63, 330, 33)];
    mediaView = [[GADMediaView alloc] initWithFrame:CGRectMake(65, 102, 250, 150)];
    Adbutton = [[UIButton alloc] initWithFrame:CGRectMake(260, 260, 80, 34)];
    Adbutton.userInteractionEnabled = NO;
    
    [nativeAdview addSubview:Adtittle];
    [nativeAdview addSubview:iconView];
    [nativeAdview addSubview:Advertiser];
    [nativeAdview addSubview:Adbody];
    [nativeAdview addSubview:mediaView];
    [nativeAdview addSubview:Adbutton];
    [self.view addSubview:nativeAdview];
    
    nativeAdview.headlineView = Adtittle;
    nativeAdview.iconView = iconView;
    nativeAdview.advertiserView = Advertiser;
    nativeAdview.bodyView = Adbody;
    nativeAdview.mediaView = mediaView;
    nativeAdview.callToActionView = Adbutton;
}


- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"%@ failed with error: %@", adLoader, error);
}

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    GADNativeAdView *native = nativeAdview;
    
    heightConstraint.active = YES;
    
    native.nativeAd = nativeAd;
    
    nativeAd.delegate = self;
    
    
    ((UILabel *)native.headlineView).text = nativeAd.headline;
    native.headlineView.hidden = nativeAd.headline ? NO : YES;
    
    native.mediaView.mediaContent = nativeAd.mediaContent;
    
    
    ((UILabel *) native.bodyView).text = nativeAd.body;
    native.bodyView.hidden = nativeAd.body ? NO : YES;
    
    
    ((UIImageView *)native.iconView).image = nativeAd.icon.image;
    native.iconView.hidden = nativeAd.icon ? NO : YES;
    
    
    ((UILabel *)native.advertiserView).text = nativeAd.advertiser;
    native.advertiserView.hidden = nativeAd.advertiser ? NO : YES;
    
    
    [((UIButton *)native.callToActionView) setTitle:nativeAd.callToAction forState:UIControlStateNormal];
    [((UIButton *)native.callToActionView) setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    native.callToActionView.hidden = nativeAd.callToAction ? NO : YES;
    
    NSLog(@"Native adapter class name: %@", nativeAd.responseInfo.adNetworkClassName);//Check the value of adNetworkClassName
}

- (void)nativeAdDidRecordImpression:(GADNativeAd *)nativeAd {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}


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


@end

取得結果



show date