본문 바로가기

ISSUE

[ISSUE] LaunchScreen 대체

LaunchScreen이 보여지고 난 후에

 

rootViewController인 TabBar에서 GoogleAdMob을 사용하여

전면 광고를 띄우는 프로세스가 있었다.

 

광고를 present 하기 전에 먼저

Google에서 부여 받은 UnitID를 사용하여

광고 load를 먼저 했었는데

가끔 가다 load하는 시간이 길어지는 상황이 발행해서

한참 있다가 전면광고가 present 되버리는 issue가 발생했다.


광고 load 시점은 앱이 실행되자 마자 

appDelegate의 didFinishLaunchingWithOptions 메소드 내부에서

하단 코드와 같이 맨 처음 load가 진행 되게끔 작성해놨었다.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let vc = MainTabVC()
    vc.interstitialAd = vc.createdAd() // 광고 Load
    .
    .
    .
    window?.rootViewController = vc
    return true
    
}

 

더이상 광고를 load 하는 시간을 단축 시킬 방법이 없다고 판단하여

가짜 launchScreenVC를 하나 만들어서 

issue를 해결했던 과정을 포스팅 하려한다.

 

 

일단 현재 LaunchScreen과 동일하게 보여줄 수 있는 

가짜 LauchVC를 하나 만들고 

rootViewContorller로 지정해주었다.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // let vc = MainTabVC()
    // vc.interstitialAd = vc.createdAd() // 광고 Load
    let launchVC = LaunchVC()
    vc.interstitialAd = vc.createdAd() // 광고 Load
    .
    .
    .
    window?.rootViewController = launchVC
    return true
    
}

 

 

그다음 TabBar 내부에 위치해 있던 광고 프로세스를 

launchVC 내부에 그대로 이전시켜

launchVC에서 광고를 load하며 present가 되도록 구현했다.

 

이렇게 하면 이제

LauchScreen( Red )이 끝나고

가짜 LauchScreen인 launchVC( Blue )가 보여지게 될것이다.

( 가독하기 편하라고 BG 세팅해주었음 )

 

 

 

그 후에 여기서 MainTabBar의 모습을 보여줘야 하는데

필자는 GADInterstitialDelegate의 메소드중

전면 광고를 닫은 직후에 호출되는 

interstitialDidDismissScreen 메소드를 활용하여

광고를 닫을때 rootViewController를 변경해주었다.

guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
let vc = MainTabVC()
window.rootViewController = vc // window의 rootViewController를 MainTabVC로 탈바꿈 
window.makeKeyAndVisible()

 

최종적으로 광고를 닫았을때

MainTabVC( 편의상 green색 적용 )가 

rootViewController로 변경된걸 볼 수 있을것이다.