首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Ads 8.0 Interstitial SwiftUI

Google Ads 8.0 Interstitial SwiftUI
EN

Stack Overflow用户
提问于 2021-02-08 08:07:13
回答 1查看 406关注 0票数 1

在SwiftUI中使用谷歌MobileAdsSDK 8.0 (iOS)的例子似乎并不多。到目前为止,我有一个Interstitial

代码语言:javascript
复制
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()
    }
}

在我的SwiftUI视图中,我创建了一个局部变量Interstitial,当执行一个操作时,我调用showAd()函数,但是,当广告显示它时,它会立即停止showAd()调用之后的代码运行。因此,我认为我需要以某种方式调用showAd(),一旦广告被取消,然后在视图中执行剩余的代码。正如您在上面看到的,Interstitial类是委托,但是我如何“警告”我的SwiftUI视图广告被驳回,以便我可以执行其余的代码?下面是我的观点。

代码语言:javascript
复制
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)
        }
    }
    
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-02 04:39:42

通过添加excaping闭包,您可以传递一个函数并执行所需的操作。

代码语言:javascript
复制
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()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66094415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档