개발 창고/iOS

[SwiftUI] AdMob 적용하기

로이제로 2023. 11. 24. 22:00
반응형


이 버전에서는 TOC를 지원하지 않습니다. (ex. 모바일)

0. 사전 작업

https://apps.admob.com/

 

AdMob

이메일 또는 휴대전화

accounts.google.com

  • AdMob 계정 생성 (필수)
  • 어플 앱스토어 등록 (선택)

 꼭 필수는 아니지만, 사전에 어플을 먼저 앱스토어에 등록 후 AdMob 작업을 진행하는 게 훨씬 더 수월합니다.


1. 새 앱 등록

새 앱 설정 #1
새 앱 설정 #2
앱 승인 메일


2. 광고 단위 생성

광고 단위 생성 #1
광고 단위 생성 #2
광고 단위 생성 #3
광고 단위 생성 #4


3. SDK 설치

# 해당 프로젝트로 이동
$> cd (프로젝트)

# CocoaPods 초기화 (최초 생성 시)
$> pod init

# CocoaPods 설정 추가
$> vi Podfile
...
pod 'Google-Mobile-Ads-SDK'
...

# 설치
$> pod install

 

cocoapods 초기화
pod 'Google-Mobile-Ads-SDK' 추가
pod install 실행
xcworkspace로 실행


4. 앱 ID 세팅

4-1. info.plist 설정

info.plist 설정

Key Type Value
GADApplicationIdentifier String
GADIsAdManagerApp Boolean YES
Privacy - Tracking Usage Description String 맞춤광고를 위해 타사 광고 추적목적으로 사용됨.
Localization native development region String Korea
SKAdNetworkItems Array <array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>4fzdc2evr5.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>4pfyvq9l8r.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>ludvb6z3bs.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cp8zw746q7.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>3sh42y64q3.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>f38h382jlk.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>hs6bdukanm.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>gta9lk7p23.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>vutu7akeur.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>y5ghdn5j9k.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>n6fk4nfna4.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>v9wttpbfk9.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>n38lu8286q.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>47vhws6wlr.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>eh6m2bh4zr.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>a2p9lx4jpn.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>22mmun2rn5.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>klf5c3l5u5.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>ppxm28t8ap.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>ecpz2srf59.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>uw77j35x4d.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>pwa73g5rt2.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>mlmmfzh3r3.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>3rd42ekr43.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>3qcr597p9d.skadnetwork</string>
</dict>
</array>

설정 결과

 


5. 배너 적용

5.1.  초기화

import SwiftUI
import GoogleMobileAds
import AppTrackingTransparency

@main
struct iPlankApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
                ATTrackingManager.requestTrackingAuthorization(completionHandler: { _ in })
            }
        }
    }
    
    init() {
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        
        // DispatchQueue 이용
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
          ATTrackingManager.requestTrackingAuthorization(completionHandler: { _ in })
        }
    }
}

 

5.2. View 생성

//
//  GoogleAdView.swift
//  ScoreBoard
//
//  Created by Royzero on 2023/09/22.

import SwiftUI
import GoogleMobileAds

struct GoogleAdView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        let viewController  = UIViewController()
        let bannerSize      = GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(UIScreen.main.bounds.width)
        let banner          = GADBannerView(adSize: bannerSize)
        banner.rootViewController = viewController
        viewController.view.addSubview(banner)
        viewController.view.frame = CGRect(origin: .zero, size: bannerSize.size)
        
        banner.adUnitID = "ca-app-pub-"  // 실제 배너 광고 단위 ID
        #if DEBUG
        banner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        #endif
        
        banner.load(GADRequest())
        return viewController
    }

    func updateUIViewController(_ viewController: UIViewController, context: Context) {

    }
}

struct GoogleAdView_Previews: PreviewProvider {
    static var previews: some View {
        GoogleAdView()
          .frame(width: UIScreen.main.bounds.width, height: GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(UIScreen.main.bounds.width).size.height)
    }
}

 

5.2. View 적용 

import SwiftUI
import GoogleMobileAds

struct ContentView: View {
    ...
    var body: some View {
        ...
        GoogleAdView()
         .frame(width: UIScreen.main.bounds.width, height: GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(UIScreen.main.bounds.width).size.height)
		...
    }
}

 


 

6. 결과 화면

상단에 광고 표시


대상 어플

https://apps.apple.com/us/app/planktime/id6472001393

 

‎PlankTime

‎플랭크 운동에 도움이 되는 타이머 입니다.

apps.apple.com

 

출처

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

 

시작하기  |  iOS  |  Google for Developers

기본 요건, 설정 세부정보, 광고 유형 개요 등 iOS용 SDK를 시작하는 방법을 알아보세요.

developers.google.com

 

반응형