「SDK8 InReadAnchor」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
行 5: | 行 5: | ||
== 加入 TADInReadAnchor == | == 加入 TADInReadAnchor == | ||
* | *匯入廣告標頭檔import TAMediaAdsFramework | ||
*在 UIViewController 中宣告 TADInReadAdAnchor 執行個體 | *在 UIViewController 中宣告 TADInReadAdAnchor 執行個體 | ||
*帶入需要呈現廣告的ViewController,初始化廣告 | *帶入需要呈現廣告的ViewController,初始化廣告 | ||
*建立Request請求廣告 | *建立Request請求廣告 | ||
== AnchorViewController.swift == | |||
<source> | |||
@implementation AnchorSampleVC | @implementation AnchorSampleVC | ||
class AnchorViewController: UIViewController { | |||
@IBOutlet weak var resultLabel: UILabel! | |||
var anchorView: TADInReadAnchor? | |||
override func viewDidLoad() { | |||
super.viewDidLoad() | |||
requestAd() | |||
} | |||
func requestAd() { | |||
let request = TADRequest() | |||
// 顯示log | |||
request.showLog = true | |||
// set gender | |||
request.gender = [_GENDER_] | |||
// Set birthday | |||
request.setBirthdayWithYear([_YEAR_], month: [_MONTH_], day: [_DAY_]) | |||
// Set location | |||
request.setLocationWithLatitude(latitude: [_LATITUDE_], longitude: :[_LONGITUDE_], accuracy: [_ACCURACY_]) | |||
anchorView = TADInReadAnchor(anchorWith: self) | |||
// 設定 AD Unit ID | |||
anchorView?.adUnitID = [_YOUR_AD_UNIT_ID_] | |||
// 必須要設定delegate | |||
anchorView?.delegate = self | |||
// 建立Request請求廣告 | |||
anchorView?.load(request) | |||
if let inread = anchorView { | |||
self.view.addSubview(inread) | |||
} | |||
} | |||
} | |||
</source> | |||
== TADInReadAdRectDelegate == | == TADInReadAdRectDelegate == | ||
<source> | |||
extension AnchorViewController: TADInReadAnchorDelegate { | |||
// 廣告取得成功 | |||
func anchorDidReceiveAd(_ anchor: TADInReadAnchor!) { | |||
} | |||
// 廣告取得失敗 | |||
func anchor(_ anchor: TADInReadAnchor!, didFailToReceiveAdWithError error: TADRequestError!) { | |||
} | |||
// 廣告即將被呈現之前被呼叫 | |||
func anchorWillShow(_ anchor: TADInReadAnchor!) { | |||
} | |||
// 廣告被關閉之後被呼叫 | |||
func anchorUserPressedCloseAd() { | |||
//user關閉anchor後 須將廣告及delegate設為nil | |||
self.anchorView?.delegate = nil; | |||
self.anchorView = nil; | |||
} | |||
// 應用程式即將進入背景執行或中止運作前被呼叫 | |||
func anchorWillLeaveApplication(_ anchor: TADInReadAnchor!) { | |||
} | } | ||
} | |||
</source> |
於 2021年8月5日 (四) 07:18 的修訂
簡介
Anchor廣告的特性是會位於ViewController正下方,圖片依照640x280、影片依照560x315的比例,在不超過畫面高度1/3內,自動fit螢幕寬度,當螢幕轉向時因高度改變會依照上述特性調整廣告尺寸。
加入 TADInReadAnchor
- 匯入廣告標頭檔import TAMediaAdsFramework
- 在 UIViewController 中宣告 TADInReadAdAnchor 執行個體
- 帶入需要呈現廣告的ViewController,初始化廣告
- 建立Request請求廣告
AnchorViewController.swift
@implementation AnchorSampleVC
class AnchorViewController: UIViewController {
@IBOutlet weak var resultLabel: UILabel!
var anchorView: TADInReadAnchor?
override func viewDidLoad() {
super.viewDidLoad()
requestAd()
}
func requestAd() {
let request = TADRequest()
// 顯示log
request.showLog = true
// set gender
request.gender = [_GENDER_]
// Set birthday
request.setBirthdayWithYear([_YEAR_], month: [_MONTH_], day: [_DAY_])
// Set location
request.setLocationWithLatitude(latitude: [_LATITUDE_], longitude: :[_LONGITUDE_], accuracy: [_ACCURACY_])
anchorView = TADInReadAnchor(anchorWith: self)
// 設定 AD Unit ID
anchorView?.adUnitID = [_YOUR_AD_UNIT_ID_]
// 必須要設定delegate
anchorView?.delegate = self
// 建立Request請求廣告
anchorView?.load(request)
if let inread = anchorView {
self.view.addSubview(inread)
}
}
}
TADInReadAdRectDelegate
extension AnchorViewController: TADInReadAnchorDelegate {
// 廣告取得成功
func anchorDidReceiveAd(_ anchor: TADInReadAnchor!) {
}
// 廣告取得失敗
func anchor(_ anchor: TADInReadAnchor!, didFailToReceiveAdWithError error: TADRequestError!) {
}
// 廣告即將被呈現之前被呼叫
func anchorWillShow(_ anchor: TADInReadAnchor!) {
}
// 廣告被關閉之後被呼叫
func anchorUserPressedCloseAd() {
//user關閉anchor後 須將廣告及delegate設為nil
self.anchorView?.delegate = nil;
self.anchorView = nil;
}
// 應用程式即將進入背景執行或中止運作前被呼叫
func anchorWillLeaveApplication(_ anchor: TADInReadAnchor!) {
}
}