Phần 2: Admob
Bài đăng này đã không được cập nhật trong 7 năm
Đầu tiên bạn cần đăng ký tài khoản Google Admob https://www.google.com/admob/ để sử dụng.
Tạo mới 1 App trên tài khoản Admob, ở app này bạn có thể lựa chọn các loại quảng cáo để dùng trong app của mình (Banner Ads, Interstitial Ads, Native Ads)

Project 3: Banner Ads
Add ad unit: Thêm 1 Ad unit, và chọn Banner

lưu lại App ID, và ad unit ID

Tạo mới 1 project và dùng cocoa pod để install Mobile Ads SDK ở trong AppDelegate.swift import thư viện
import GoogleMobileAds
Tiếp theo ở application**(_:didFinishLaunchingWithOptions:)**
GADMobileAds.configure(withApplicationID: "ca-app-pub-2309631356952308~9523587078")
Tiếp theo trên giao diện của app, add thêm 1 view: nơi banner quảng cáo xuất hiện

Lưu ý:
- Tạo BannerView có heigh = 50 px, add constraints để nó ở nơi bạn muốn (mình để ở Bottom)
- Chọn Class GADBannerView
- Khi kéo Outlet vào cũng để thay UIView thành GADBannerView
Tiếp theo trong ViewDidLoad()
bannerView.adUnitID = "ca-app-pub-2309631356952308/7312378735"
bannerView.rootViewController = self
let request = GADRequest()
bannerView.load(request)
OK. Vậy là đủ, bây giờ bạn chạy ứng dụng của mình để xem kết quả nhé

Project 4: Interstitial Ads
Tạo 1 project và install Mobile Ads SDK như với Banner Ads
Add ad unit Interstitial trên Google Admob

ở trong AppDelegate.swift vẫn import thư viện và configure cho Admob import thư viện
import GoogleMobileAds
Tiếp theo ở application**(_:didFinishLaunchingWithOptions:)**
GADMobileAds.configure(withApplicationID: "ca-app-pub-2309631356952308~9523587078")
Tạo giao diện gồm có button, khi tap vào thì sẽ hiển thị quảng cáo

Tạo biến interstital
var interstitial: GADInterstitial!
Viết action cho button
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready!")
}
Viết hàm tạo và load interstitial
func createAndLoadInterstitialAdmob() {
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-2309631356952308/2038449872")
let request = GADRequest()
request.testDevices = [kGADSimulatorID, "08fc5f0b45a13049a73bb4dff363b9b4"]
interstitial.load(request)
}
Gọi đến hàm tạo là load Interstitial
createAndLoadInterstitialAdmob()
OK. Bây giờ hãy chạy app để xem thành quả của mình nào

Peaceeee!
All rights reserved