Google AdMob in SwiftUI

Updated:

SwiftUI 로 만든 App 내에 Google AdMob 을 설치, 및 적용을 구현하는 과정입니다

🔷 1.Install AdMob SKD with CocoaPods

Admob ios quick start guide - https://developers.google.com/admob/ios/quick-start?hl=ko

  • 먼저 CocoaPods 를 통해서 google Mobile ads SDK 를 설치합니다

pod init 해서 프로젝트 root 경로에 생성된 Podfile 실행 한 후 , pod 'Google-Mobile-Ads-SDK'붙여 넣고 pod install 실행

그러면 Google-Mobile-Ads-SDK 가 설치가 된다

image

🔷 2.Initialize AdMob in SwiftUI App

@main 앱 페이지 가서 GADMobileAds 를 초기화 시킴

GADMobileAds.sharedInstance().start(completionHandler: nil)

import SwiftUI
import GoogleMobileAds

@main
struct EndangeredAnimalsKRApp: App {
	init() {
		GADMobileAds.sharedInstance().start(completionHandler: nil)
	}
	var body: some Scene {
		WindowGroup {
			SplashScreen()
		}
	}
}

🔷 3.Update Info, Plist with AdMob ID

🔶 Update your info.plist

프로젝트 내에 info.list 에 source code 형태로 open as 해서 아래의 코드를 붙여 넣기 한다

<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
<key>SKAdNetworkItems</key>
  <array>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>cstr6suwn9.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>4fzdc2evr5.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>2fnua5tdw4.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>ydx93a7ass.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>5a6flpkh64.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>p78axxw29g.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>v72qych5uu.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>c6k4g5qg8m.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>s39g8k73mm.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>3qy4746246.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>3sh42y64q3.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>f38h382jlk.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>hs6bdukanm.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>prcb7njmu6.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>v4nxqhlyqp.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>wzmmz9fp6w.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>yclnxrl5pm.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>t38b2kh725.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>7ug5zh24hu.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>9rd848q2bz.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>n6fk4nfna4.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>kbd757ywx3.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>9t245vhmpl.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>4468km3ulz.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>2u9pt9hc89.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>8s468mfl3y.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>av6w8kgt66.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>klf5c3l5u5.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>ppxm28t8ap.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>424m5254lk.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>uw77j35x4d.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>578prtvx9j.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>4dzt52r2t5.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>e5fvkxwrpn.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>8c4e2ghe7u.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>zq492l623r.skadnetwork</string>
    </dict>
    <dict>
      <key>SKAdNetworkIdentifier</key>
      <string>3qcr597p9d.skadnetwork</string>
    </dict>
  </array>

image

🔷 4.Change the sample AdMob ID with your AdMob App ID

🔷 5.Integrate AdMob UIKit View into SwiftUI

//  BannerAd.swift

import SwiftUI
import GoogleMobileAds

struct BannerAd: UIViewRepresentable {

	var unitID: String

	func makeCoordinator() -> Coordinator {
		// For Implementing Delegates..
		return Coordinator()
	}

	func makeUIView(context: Context) -> GADBannerView{
		let adView = GADBannerView(adSize: GADAdSizeBanner)

		adView.adUnitID = unitID
		adView.rootViewController = UIApplication.shared.getRootViewController()

		adView.load(GADRequest())

		return adView
	}

	func updateUIView(_ uiView: GADBannerView, context: Context) {

	}

	class Coordinator: NSObject, GADBannerViewDelegate {
		func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
			print("bannerViewDidReceiveAd")
		}

		func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
			print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
		}

		func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
			print("bannerViewDidRecordImpression")
		}

		func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
			print("bannerViewWillPresentScreen")
		}

		func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
			print("bannerViewWillDIsmissScreen")
		}

		func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
			print("bannerViewDidDismissScreen")
		}
	}
}

// Extending Application to get RootView..
extension UIApplication {
	func getRootViewController() -> UIViewController {

		guard let screen = self.connectedScenes.first as? UIWindowScene else {
			return .init()
		}

		guard let root = screen.windows.first?.rootViewController else {
			return .init()
		}

		return root
	}
}

🔷 6.Change sample add ID with your Ad ID

BannerAd(unitID: "ca-app-pub-8787171365157933/2845789069")

🗃 Reference

Categories:

Updated:

Leave a comment