Admob 的回调不会在 Swift 中每次触发
Admob's callback not firing everytime in Swift
我在 Swift 中使用 SwiftUI 框架和 SwiftUI 应用程序生命周期制作了一个应用程序。
我用那个 class 来管理插页式广告:
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 的真实设备或模拟器上使用该应用程序时,回调永远不会起作用。
可能是什么问题?
提前致谢,
翁文
尝试以下操作:
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)")
}
}
我在 Swift 中使用 SwiftUI 框架和 SwiftUI 应用程序生命周期制作了一个应用程序。
我用那个 class 来管理插页式广告:
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 的真实设备或模拟器上使用该应用程序时,回调永远不会起作用。
可能是什么问题?
提前致谢, 翁文
尝试以下操作:
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)")
}
}