3

Seems like there isn't many examples of using Google MobileAdsSDK 8.0 (iOS) with SwiftUI. So far I have a class Interstitial

import GoogleMobileAds
import UIKit
    
final class Interstitial:NSObject, GADFullScreenContentDelegate{
    var interstitial:GADInterstitialAd!
    
    override init() {
        super.init()
        LoadInterstitial()
    }
    
    func LoadInterstitial(){
        let req = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "...", request: req) { ad, error in
            self.interstitial = ad
            self.interstitial.fullScreenContentDelegate = self
        }
        
    }
    
    func showAd(){
        if self.interstitial != nil {
            let root = UIApplication.shared.windows.first?.rootViewController
            self.interstitial.present(fromRootViewController: root!)
        }
    }
    
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        LoadInterstitial()
    }
}

In my SwiftUI view i create a local variable Interstitial, and when an action is performed I call the showAd() function however when the ad displays it stops the code immediately following the showAd() call from running. So I think I need to somehow call showAd() and once the ad is dismissed then perform the remainder of my code in the view. As you can see above the Interstitial class is the delegate, but how do I "alert" my SwiftUI view that the ad was dismissed so I can execute the rest of the code? Below is my View.

import SwiftUI

struct MyView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext

    var interstitial : Interstitial = Interstitial()

    
    var body: some View {
        VStack{
            //... Display content
        }
        .navigationBarItems(trailing:
            HStack{
                Button(action: actionSheet) {
                    Image(systemName: "square.and.arrow.up")
                }
                
            }
        )
    }
    
    func showAd(){
        interstitial.showAd()
    }
    
    func actionSheet() {
        showAd()
        let data = createPDF()
        let temporaryFolder = FileManager.default.temporaryDirectory
        let fileName = "export.pdf"
        let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
        do {
            try data.write(to: temporaryFileURL)
            let av = UIActivityViewController(activityItems: [try URL(resolvingAliasFileAt: temporaryFileURL)], applicationActivities: nil)
            UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)

        } catch {
            print(error)
        }
    }
    
}
snoop168
  • 394
  • 3
  • 16

1 Answers1

0

By adding an excaping closure, you pass a function and perform the needed actions.

final class InterstitialAd: NSObject, GADFullScreenContentDelegate {
    var completion: () -> Void
    var interstitial: GADInterstitialAd!
    
    init(completion: @escaping () -> Void) {
        self.completion = completion
        
        super.init()
        
        LoadInterstitialAd()
    }
    
    func LoadInterstitialAd() {
        let req = GADRequest()
        GADInterstitialAd.load(withAdUnitID: Constants.AdmobIDs.saveImageBlockID, request: req) { ad, error in
            self.interstitial = ad
            self.interstitial.fullScreenContentDelegate = self
        }
        
    }
    
    func show() {
        if self.interstitial != nil {
            let root = UIApplication.shared.windows.first?.rootViewController
            self.interstitial.present(fromRootViewController: root!)
        }
    }
    
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        LoadInterstitialAd()
        completion()
    }
}

Daniel
  • 50
  • 1
  • 7