Music_Player3/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_AdMobManager.swift
2024-06-15 23:52:09 +08:00

276 lines
10 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MP_AdMobManager.swift
// relax.offline.mp3.music
//
// Created by Mr.Zhou on 2024/6/14.
//
import UIKit
import GoogleMobileAds
///广
class MP_AdMobManager: NSObject, GADFullScreenContentDelegate {
static let shared = MP_AdMobManager()
private let sharedInstance = GADMobileAds.sharedInstance()
///广(50)
private let expirationTime:TimeInterval = 3000
//广
private func wasAdexpirationTime(_ date:Date?) -> Bool {
guard let loadTime = date else { return false }
// Check if ad was loaded more than four hours ago.
return Date().timeIntervalSince(loadTime) < expirationTime
}
//MARK: - 广
///广
private var interstitialSwitch:Bool = false
//
func setInterstitialSwitch(_ status:Bool) {
interstitialSwitch = status
}
private override init() {
super.init()
//广
completeOpenAdBlock = {
[weak self] in
guard let self = self, interstitialSwitch == true else {return}
//广
appOpenAd = nil
isShowingOpenAd = false
loadOpenAdTime = nil
//广
interstitialSwitch = false
//广
loadOpenAd(.HOST) { status in
if status == true {
print("新的开屏广告加载成功")
}else {
print("开屏广告加载失败了")
}
}
}
}
///广
func start() {
sharedInstance.start { status in
print("App启动开始加载广告")
}
}
//MARK: -
//广ID
private let OpenICEID:String = "ca-app-pub-3940256099942544/5575463023"
//广ID
private let OpenHOSTID:String = "ca-app-pub-3940256099942544/5575463023"
//广
enum OpenType:Int {
//
case ICE = 0
//
case HOST = 1
var title:String{
switch self {
case .ICE:
return "是冷启动开屏广告"
case .HOST:
return "是热启动开屏广告"
}
}
}
//广
private var appOpenAd:GADAppOpenAd?
//广
private var isLoadingOpenAd:Bool = false
//广
private var isShowingOpenAd:Bool = false
//广
private var loadOpenAdTime:Date?
//广
var completeOpenAdBlock:(() -> Void)?
//广
func loadOpenAd(_ type:OpenType,completion: @escaping (Bool) -> Void) {
// 广广
if isLoadingOpenAd || isOpenAdAvailable() {
// 广广
completion(false)
return
}
isLoadingOpenAd = true
var UUID:String
switch type {
case .ICE:
UUID = OpenICEID
case .HOST:
UUID = OpenHOSTID
}
// 使 GADAppOpenAd load completion handler广
GADAppOpenAd.load(withAdUnitID: UUID, request: GADRequest()) { ad, error in
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if let error = error {
print("加载开屏广告失败,失败原因: \(error.localizedDescription)")
self.isLoadingOpenAd = false
completion(false)
} else {
self.appOpenAd = ad
self.isLoadingOpenAd = false
//
self.appOpenAd?.fullScreenContentDelegate = self
//
self.loadOpenAdTime = Date()
completion(true)
}
}
}
}
///广
func showOpenAdIfAvailable(_ type:OpenType, completion:((GADAppOpenAd) -> Void)?) {
// 广广广
guard !interstitialSwitch, !isShowingOpenAd else { return }
// 广广
if !isOpenAdAvailable() {
loadOpenAd(type) { [weak self] success in
guard let self = self else { return }
if success {
self.showOpenAdIfAvailable(type, completion: completion)
}
}
return
}
//广
if let ad = appOpenAd {
isShowingOpenAd = true
//
if let block = completion {
block(ad)
}else {
interstitialSwitch = true
ad.present(fromRootViewController: nil)
}
}
}
//广
func isOpenAdAvailable() -> Bool {
return appOpenAd != nil && wasAdexpirationTime(loadOpenAdTime)
}
//MARK: -
//广ID
private let SearchINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//广ID
private let SearchNATIVEID:String = "ca-app-pub-1371732277241593/5674216970"
///广
private var searchInterstitialAd:GADInterstitialAd?
///广
private var isLoadingSearchInterstitialAd:Bool = false
///广
private var isShowingSearchInterstitialAd:Bool = false
///
private var loadSearchInterstitialAdTime:Date?
//广
func loadSearchInterstitialAd(completion: @escaping (Bool) -> Void) {
// 广广
if isLoadingSearchInterstitialAd || isSearchInterstitialAdAvailable() {
// 广广
completion(false)
return
}
isLoadingSearchInterstitialAd = true
//广
GADInterstitialAd.load(withAdUnitID: SearchINSERTID, request: GADRequest()) { ad, error in
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if let error = error {
print("加载搜索插页广告失败,失败原因: \(error.localizedDescription)")
self.isLoadingSearchInterstitialAd = false
completion(false)
} else {
self.searchInterstitialAd = ad
self.isLoadingSearchInterstitialAd = false
//
self.searchInterstitialAd?.fullScreenContentDelegate = self
//
self.loadSearchInterstitialAdTime = Date()
completion(true)
}
}
}
}
///广
func showSearchInterstitialAdIfAvailable(completion:((GADInterstitialAd) -> Void)?) {
// 广广广
guard !interstitialSwitch, !isShowingSearchInterstitialAd else { return }
// 广广
if !isSearchInterstitialAdAvailable() {
loadSearchInterstitialAd { [weak self] success in
guard let self = self else { return }
if success {
self.showSearchInterstitialAdIfAvailable(completion: completion)
}
}
return
}
//广
if let ad = searchInterstitialAd {
isShowingSearchInterstitialAd = true
//
if let block = completion {
block(ad)
}else {
interstitialSwitch = true
ad.present(fromRootViewController: nil)
}
}
}
//广
func isSearchInterstitialAdAvailable() -> Bool {
return appOpenAd != nil && wasAdexpirationTime(loadSearchInterstitialAdTime)
}
//MARK: -
//广ID
private let PlayerINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//广ID
private let SwitchINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//广ID
private let LoadINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//MARK: -
//ID
private let LibraryINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//ID
private let LibraryNATIVEID:String = "ca-app-pub-1371732277241593/4683255855"
//MARK: -
//ID
private let GlobalINSERTID:String = "ca-app-pub-3940256099942544/4411468910"
//MARK: - 广 GADFullScreenContentDelegate
//广
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
if ad === appOpenAd {
print("当前展示的覆盖型广告是开屏广告广告ID--\(appOpenAd!.adUnitID)")
}
}
//广
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
if ad === appOpenAd {
print("当前消失的覆盖型广告是开屏广告广告ID--\(appOpenAd!.adUnitID)")
if completeOpenAdBlock != nil {
completeOpenAdBlock!()
}
}
}
//广
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
if ad === appOpenAd {
print("覆盖型广告出错广告ID--\(appOpenAd!.adUnitID)")
if completeOpenAdBlock != nil {
completeOpenAdBlock!()
}
}
}
}