修复广告bug
This commit is contained in:
parent
98191889a0
commit
d341359a51
@ -73,6 +73,11 @@ class VPAppOpenAdManager: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var isLoading = false
|
private var isLoading = false
|
||||||
|
private var isShowing = false
|
||||||
|
///允许自动展示
|
||||||
|
private var needAutoShow = false
|
||||||
|
|
||||||
|
private var timeOutTimer: Timer?
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
NotificationCenter.default.removeObserver(self)
|
NotificationCenter.default.removeObserver(self)
|
||||||
@ -84,6 +89,18 @@ class VPAppOpenAdManager: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func showAdIfAvailable() {
|
func showAdIfAvailable() {
|
||||||
|
|
||||||
|
if self.appOpenAd?.isReady == true {
|
||||||
|
self.showAd()
|
||||||
|
} else {
|
||||||
|
self.timeOutTimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(handleTimeOutTimer), userInfo: nil, repeats: false)
|
||||||
|
|
||||||
|
self.needAutoShow = true
|
||||||
|
self.loadAd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadAd() {
|
||||||
guard !isLoading else { return }
|
guard !isLoading else { return }
|
||||||
|
|
||||||
self.isLoading = true
|
self.isLoading = true
|
||||||
@ -91,10 +108,32 @@ class VPAppOpenAdManager: NSObject {
|
|||||||
appOpenAd?.loadAd()
|
appOpenAd?.loadAd()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func showAd() {
|
||||||
|
guard self.appOpenAd?.isReady == true else { return }
|
||||||
|
guard !self.isShowing else { return }
|
||||||
|
|
||||||
|
|
||||||
|
self.isShowing = true
|
||||||
|
self.appOpenAd?.showAd()
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func handleTimeOutTimer() {
|
||||||
|
self.needAutoShow = false
|
||||||
|
cleanTimer()
|
||||||
|
|
||||||
|
let error = NSError(domain: "time-out", code: -1)
|
||||||
|
self.delegate?.appOpenAdManager?(adManager: self, didOtherFail: error)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func cleanTimer() {
|
||||||
|
self.timeOutTimer?.invalidate()
|
||||||
|
self.timeOutTimer = nil
|
||||||
|
}
|
||||||
|
|
||||||
private func clean() {
|
private func clean() {
|
||||||
appOpenAd = nil
|
appOpenAd = nil
|
||||||
delegate = nil
|
delegate = nil
|
||||||
isLoading = false
|
isShowing = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +142,8 @@ extension VPAppOpenAdManager: VPAppOpenAdDelegate {
|
|||||||
|
|
||||||
///广告加载失败
|
///广告加载失败
|
||||||
func appOpenAd(ad: VPAppOpenAd, didLoadFail error: Error) {
|
func appOpenAd(ad: VPAppOpenAd, didLoadFail error: Error) {
|
||||||
|
cleanTimer()
|
||||||
|
isLoading = false
|
||||||
requestStatAd(type: "load_failed", errorMsg: error.localizedDescription)
|
requestStatAd(type: "load_failed", errorMsg: error.localizedDescription)
|
||||||
|
|
||||||
self.delegate?.appOpenAdManager?(adManager: self, didLoadFail: error)
|
self.delegate?.appOpenAdManager?(adManager: self, didLoadFail: error)
|
||||||
@ -111,13 +152,18 @@ extension VPAppOpenAdManager: VPAppOpenAdDelegate {
|
|||||||
}
|
}
|
||||||
///广告加载成功
|
///广告加载成功
|
||||||
func appOpenAdDidLoadFinish(ad: VPAppOpenAd) {
|
func appOpenAdDidLoadFinish(ad: VPAppOpenAd) {
|
||||||
|
self.isLoading = false
|
||||||
self.delegate?.appOpenAdManagerDidLoadFinish?(adManager: self)
|
self.delegate?.appOpenAdManagerDidLoadFinish?(adManager: self)
|
||||||
|
|
||||||
self.appOpenAd?.showAd()
|
if needAutoShow {
|
||||||
|
self.needAutoShow = false
|
||||||
|
self.showAd()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
///广告展示失败
|
///广告展示失败
|
||||||
func appOpenAd(ad: VPAppOpenAd, didDisplayFail error: Error) {
|
func appOpenAd(ad: VPAppOpenAd, didDisplayFail error: Error) {
|
||||||
|
cleanTimer()
|
||||||
requestStatAd(type: "show_failed", errorMsg: error.localizedDescription)
|
requestStatAd(type: "show_failed", errorMsg: error.localizedDescription)
|
||||||
self.delegate?.appOpenAdManager?(adManager: self, didDisplayFail: error)
|
self.delegate?.appOpenAdManager?(adManager: self, didDisplayFail: error)
|
||||||
clean()
|
clean()
|
||||||
@ -158,6 +204,7 @@ extension VPAppOpenAdManager {
|
|||||||
|
|
||||||
|
|
||||||
@objc private func didEnterBackgroundNotification() {
|
@objc private func didEnterBackgroundNotification() {
|
||||||
|
guard self.isShowing else { return }
|
||||||
|
|
||||||
self.requestStatAd(type: "Interrupt", errorMsg: nil)
|
self.requestStatAd(type: "Interrupt", errorMsg: nil)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user