iOS/StoryBoard

Google AdMob을 이용해서 iOS앱에 광고달기

소재훈 2022. 2. 19. 20:11

Google AdMob의 다음 iOS공식문서를 참조하여 광고를 적용할 수 있다.

 

시작하기  |  iOS  |  Google Developers

iOS 앱을 제작 중인 AdMob 게시자를 위한 모바일 광고 SDK입니다.

developers.google.com

 

iOS에서 구글 애드몹을 사용하기 위해서는 먼저 PodFile에 라이브러리를 추가해야한다.

pod 'Google-Mobile-Ads-SDK'

'Google-Mobile-Ads-SDK'는 iOS 10.0부터 지원되므로, iOS 플랫폼을 10.0이상의 버전으로 지정해주자!!

나는 iOS13으로 지정해주었다.

platform :ios, '13.0'

target 'MyAdMobTest' do
  	use_frameworks!
    pod 'Google-Mobile-Ads-SDK'
end

PodFile을 작성한 후 터미널에 다음 명령으로 라이브러리를 설치한다.

pod install --repo-update

 

이제 AdMob과 앱을 연결해보자!!

문서에 있는 것처럼 Info.plist파일에 GADApplicationIdentifier항목을 추가하고, 문서에 나와있는 문자열을 입력해주자.

iOS 에서 앱의 라이프 사이클이 AppDelegate에서 시작되기 때문에, AppDelegate.swift에서 Google Admob을 import해준다.

 

우리는 여러 광고 유형중에서 배너광고를 추가할 것이기 때문에 배너관련 문서를 살펴보자.

먼저 배너를 출력할 뷰 컨트롤러에 GADBannerView 오브젝트를 추가해주고, 배너의 사이즈, contraint등을 설정해준다.

GoogleMobildAds를 import 해주는 것도 잊지 말자.

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  var bannerView: GADBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // In this case, we instantiate the banner with desired ad size.
    bannerView = GADBannerView(adSize: kGADAdSizeBanner)

    addBannerViewToView(bannerView)
  }

  func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
      [NSLayoutConstraint(item: bannerView,
                          attribute: .bottom,
                          relatedBy: .equal,
                          toItem: bottomLayoutGuide,
                          attribute: .top,
                          multiplier: 1,
                          constant: 0),
       NSLayoutConstraint(item: bannerView,
                          attribute: .centerX,
                          relatedBy: .equal,
                          toItem: view,
                          attribute: .centerX,
                          multiplier: 1,
                          constant: 0)
      ])
   }
   
}

여기서 bannerView = GADBannerView(adSize: kGADAdSizeBanner) 는 배너의 사이즈를 지정하는 항목이다.

kGADAdSizeBanner 는 320 × 50의 사이즈를 나타내며, 이외에도 다음과 같은 사이즈 들이 정의되어있다.

 

배너 광고  |  iOS  |  Google Developers

배너 광고 Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mo

developers.google.com

미리 지정된 사이즈가 아니라 사용자가 지정한 사이즈를 사용하고 싶다면 아래와 같이 설정해 줄 수 잇다.

나는 어째서인지 지정된 키워드가 정의도지 않았다는 에러가 발생여 수동으로 설정해주었다...ㅠㅠ

let adSize = GADAdSizeFromCGSize(CGSize(width: 300, height: 50))

 

그리고 배너뷰에는 고유의 배너 아이디와 rootViewController를 선택해야한다.

배너아이디는 구글에서 제공한 배너를 나타낸다고 한다!! 그리고 아래의 배너아이디는 구글의 테스트 배너를 나타낸다

rootViewController는 광고가 나타날 때 오버레이를 사용하는 뷰 컨트롤러를 나타낸다. 보통 배너가 정의된 뷰 컨트롤러에서 사용한다고 한다.

 

load 메서드를 사용해서 애드배너를 활성화 시킬 수 있고, 

delegate메서드를 사용하기 위해서 bannerView의 delegate를 현재 뷰 컨트롤러로 해준다.

override func viewDidLoad() {
  	super.viewDidLoad()
  	...

    // 광고 배너의 아이디를 설정
    bannerView.adUnitID = "ca-app-pub-5115370026469477/7894440928"
    bannerView.rootViewController = self

    // 광고 로드
    bannerView.load(GADRequest())

    // 델리게이트 설정
    bannerView.delegate = self
}

 

이제 구글의 테스트 배너가 아닌 진짜 내앱의 광고 배너를 등록해보자.

Google AdMob에 연결하고, 내앱을 등록해야한다.

내 앱을 등록하고 나면 

이러한 코드들을 볼 수 있는데, 이 코드들을 위에서 제공해준 코드들 대신에 입력해주면 된다.

GADApplicationIdentifier와 bannerView.adUnitID 부분에 지정해주면된다.

 

코드를 지정해 준다음 앱을 실행해도 광고 배너를 출력해주지 못할 수도 있다.

처음에 코드를 생성한 이후에는 한시간 이상을 기다릴 수도 있다고 한다.

시간이 흐른후 앱을 실행시켜주면 광고 배너가 실행되는 것을 볼 수 있다.

콘솔 에러

publisher data not found라는 데이터를 찾을 수 없다는 에러가 나오는데....밑의 링크를 따라가보면 설명되어 있다.

또한 AdMob에 가입한지 얼마 되지 않았다면 저 에러가 발생할 수도 있다고 한다.

 

IOS - Google Admob - Error Domain=com.google.ads Code=1 "Request Error: No ad to show."

I spent a day to research this issue, but the result is till nothing. I need your helps, please. I have an issue about displaying my ADMOB in my iOS app as below : When I use test ad ID from goog...

stackoverflow.com