簡體   English   中英

SwiftUI - AdMob 插頁式廣告僅在 iOS 16 上崩潰

[英]SwiftUI - AdMob Interstitial ad crashes only on iOS 16

我有一個視圖,其中顯示了一個插頁式廣告。 在 iOS 15 上一切正常,但在 iOS 16 上,應用程序崩潰並出現以下錯誤:

SwiftUI/UIViewControllerRepresentable.swift:332: Fatal error: UIViewControllerRepresentables must be value types: InterstitialAdView 2022-09-22 09:33:06.740743+0200 HowRich[47572:2135353] SwiftUI/UIViewControllerRepresentable.swift:332: Fatal error: UIViewControllerRepresentables must be值類型:InterstitialAdView (lldb)

在哪里有@main 我得到這個錯誤:

在此處輸入圖像描述

代碼如下:

InterstitialAdsManager.swift

import GoogleMobileAds
import SwiftUI
import UIKit

class InterstitialAd: NSObject {
    var interstitialAd: GADInterstitialAd?
    
    static let shared = InterstitialAd()
    
    func loadAd(withAdUnitId id: String) {
        let req = GADRequest()
        GADInterstitialAd.load(withAdUnitID: id, request: req) { interstitialAd, err in
            if let err = err {
                print("Failed to load ad with error: \(err)")
                return
            }
            
            self.interstitialAd = interstitialAd
        }
    }
}

final class InterstitialAdView: NSObject, UIViewControllerRepresentable, GADFullScreenContentDelegate {
    
    let interstitialAd = InterstitialAd.shared.interstitialAd
    @Binding var isPresented: Bool
    var adUnitId: String
    
    init(isPresented: Binding<Bool>, adUnitId: String) {
        self._isPresented = isPresented
        self.adUnitId = adUnitId
        super.init()
        
        interstitialAd?.fullScreenContentDelegate = self
    }
    
    func makeUIViewController(context: Context) -> UIViewController {
        let view = UIViewController()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
            self.showAd(from: view)
        }
        
        return view
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
    
    func showAd(from root: UIViewController) {
        
        if let ad = interstitialAd {
            ad.present(fromRootViewController: root)
        } else {
            print("Ad not ready")
            self.isPresented.toggle()
        }
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        InterstitialAd.shared.loadAd(withAdUnitId: adUnitId)
        
        isPresented.toggle()
    }
}

struct FullScreenModifier<Parent: View>: View {
    @Binding var isPresented: Bool
    
    var adUnitId: String
    var parent: Parent
    
    var body: some View {
        ZStack {
            parent
            
            if isPresented {
                EmptyView()
                    .edgesIgnoringSafeArea(.all)
                
                InterstitialAdView(isPresented: $isPresented, adUnitId: adUnitId)
            }
        }
        .onAppear {
           InterstitialAd.shared.loadAd(withAdUnitId: adUnitId)
        }
    }
}

extension View {
    public func presentInterstitialAd(isPresented: Binding<Bool>, adUnitId: String) -> some View {
        FullScreenModifier(isPresented: isPresented, adUnitId: adUnitId, parent: self)
    }
}

帶有廣告的視圖:

struct CheckoutView: View {
   
    @State var showAd = false

  var body: some View {
        ScrollView {
            ZStack {
                VStack {
                  //The view
                   }
                    .onAppear {
            if UserDefaults.standard.string(forKey: "AdCounter") == "0" && UserDefaults.standard.bool(forKey: "AdFree") == false {
                showAd = true
                UserDefaults.standard.setValue("1", forKey: "AdCounter")
            }
            UserDefaults.standard.setValue("0", forKey: "AdCounter")
         }
        .presentInterstitialAd(isPresented: $showAd, adUnitId: myIntersId)
}
}
}

我已將所有 pod 更新為最新的可用版本(Google 移動廣告 SDK 是 9.11.0 版)。 是什么導致 iOS 16 崩潰,我該如何解決? 謝謝

解決方法是改變

final class InterstitialAdView: NSObject, UIViewControllerRepresentable, GADFullScreenContentDelegate {

進入

struct InterstitialAdView: NSObject, UIViewControllerRepresentable, GADFullScreenContentDelegate {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM