提问人:Onnwen 提问时间:4/7/2021 最后编辑:Onnwen 更新时间:4/7/2021 访问量:387
Admob 的回调不是每次都在 Swift 中触发
Admob's callback not firing everytime in Swift
问:
我使用 SwiftUI 框架和 SwiftUI App 生命周期在 Swift 中制作了一个应用程序。
我使用该类来管理插页式广告:
class Interstitial : NSObject, GADFullScreenContentDelegate, UIApplicationDelegate{
var interstitial : GADInterstitialAd?
let adUnitID: String = "ca-app-pub-3940256099942544/4411468910"
override init() {
super.init()
loadInterstitial()
}
func loadInterstitial(){
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID: adUnitID, request: request) { [self] (ad, err) in
if(err != nil){
print(String(describing:err?.localizedDescription))
}
else {
interstitial = ad
interstitial?.fullScreenContentDelegate = self
}
}
}
func showAd(){
let root = UIApplication.shared.windows.first?.rootViewController
if interstitial != nil {
interstitial!.present(fromRootViewController: root!)
}
else {
nc.post(name: Notification.Name("UserClosedAd"), object: nil)
}
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("ad closed")
nc.post(name: Notification.Name("UserClosedAd"), object: nil)
return
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print(error.localizedDescription)
print("ad failed to be presented")
nc.post(name: Notification.Name("UserClosedAd"), object: nil)
return
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("ad presented")
return
}
}
我以这种方式展示广告:
let pub = NotificationCenter.default
.publisher(for: NSNotification.Name("UserClosedAd"))
var interstitial : Interstitial = Interstitial()
var body: some View {
Button(action:
{
self.interstitial.showAd()
}) {
Image(systemName: "text.bubble")
.foregroundColor(.red)
Text("Show ad")
}
.font(.system(size: 30, weight: .semibold, design: .rounded))
.padding(30)
.onAppear(perform: {
interstitial.loadInterstitial()
})
.onReceive(pub, perform: { _ in
newView = true
print("Ad closed")
})
}
问题是有时回调有效,但有时无效。当我在未连接到 Xcode 的真实设备或模拟器上使用该应用程序时,回调永远不会起作用。
可能是什么问题?
提前致谢, 翁文
答:
0赞
foolbear
4/7/2021
#1
请尝试以下操作:
struct TestView: View {
@ObservedObject var interstitial: FoolGADInterstitial = FoolGADInterstitial(adUnitId: "xxxx")
var body: some View {
Text("123")
.onAppear(perform: onAppear)
}
func onAppear() {
#if PLUSVERSION
#else
guard manager.shouldShowInterstitialAd else { return }
interstitial.showAd()
#endif
}
}
class FoolGADInterstitial: NSObject, GADFullScreenContentDelegate, ObservableObject {
var adUnitId: String = ""
var interstitial : GADInterstitialAd? = nil
init(adUnitId: String) {
super.init()
self.adUnitId = adUnitId
loadInterstitial()
}
func loadInterstitial() {
GADInterstitialAd.load(withAdUnitID: adUnitId, request: GADRequest()) { [weak self] (ad, err) in
if let err = err {
foolPrint("load error, \(err.localizedDescription)")
return
}
guard let self = self else { return }
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
}
}
func showAd() {
guard let ad = self.interstitial, let root = UIApplication.shared.windows.last?.rootViewController else { return }
foolPrint("showAd done.")
ad.present(fromRootViewController: root)
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
foolPrint("Ad dismissed")
loadInterstitial()
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
foolPrint("present failed, \(error.localizedDescription)")
}
}
评论
0赞
Onnwen
4/7/2021
非常感谢你,这很有效,但只有当我第二次点击按钮时。第一次回调永远不起作用......你知道那会是什么吗?
0赞
foolbear
4/7/2021
因为从 admob 下载广告需要时间。如果 self.interstitial = nil 在 0.2 秒后重试,则可以重试。
评论