Music_Player3/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_ADSimpleManager.swift
2024-12-13 18:47:39 +08:00

257 lines
8.3 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_ADSimpleManager.swift
// relax.offline.mp3.music
//
// Created by Mr.Zhou on 2024/9/19.
//
import UIKit
import AVFoundation
import GoogleMobileAds
import AppLovinAdapter
import AppLovinSDK
import LiftoffMonetizeAdapter
import VungleAdsSDK
import PangleAdapter
import PAGAdSDK
import IronSourceAdapter
import IronSource
import MintegralAdapter
import MTGSDK
import ChartboostSDK
import ChartboostAdapter
//广
enum OpenType:Int {
//
case ICE = 0
//
case HOST = 1
var title:String{
switch self {
case .ICE:
return "是冷启动开屏广告"
case .HOST:
return "是热启动开屏广告"
}
}
}
///广
class MP_ADSimpleManager: NSObject {
static let shared = MP_ADSimpleManager()
//广 falseAdMobtureAppLovin
var platform:Bool{
if let newValue = UserDefaults.standard.object(forKey: "platform") as? Bool {
return newValue
}else {
return false
}
}
///广
var openAdStatus:Bool{
return !MP_IAPManager.shared.isVIP
}
///广
private var isMuted:Bool {
if let bool = UserDefaults.standard.object(forKey: "isAdMuted") as? Bool {
return bool
}else {
return false
}
}
///使广
var internalAdStatus:Bool = true
///广
func setOpenAdStatus(_ bool:Bool) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
UserDefaults.standard.set(bool, forKey: "UserVIPLevels")
}
}
///广
private var interstitialSwitch:Bool {
get {
if platform {
return MP_AppLovinManager.shared.getInterstitialSwitch()
}else {
return MP_AdMobManager.shared.getInterstitialSwitch()
}
}
}
///
func getInterstitialSwitch() -> Bool {
return interstitialSwitch
}
override init() {
super.init()
NotificationCenter.notificationKey.add(observer: self, selector: #selector(netWorkReachableAction(_:)), notificationName: .net_switch_reachable)
DispatchQueue.main.async {
//
GADMobileAds.sharedInstance().audioVideoManager.audioSessionIsApplicationManaged = true
//广0.8
GADMobileAds.sharedInstance().applicationVolume = 0.8
}
}
deinit{
NotificationCenter.default.removeObserver(self)
}
///广
func isAdMuted() {
guard isMuted == true else {
return
}
//
DispatchQueue.main.async {
if MP_PlayerManager.shared.getPlayState() == .Playing {
if GADMobileAds.sharedInstance().applicationMuted == false {
GADMobileAds.sharedInstance().applicationMuted = true
GADMobileAds.sharedInstance().applicationVolume = 0.1
print("播放中,静音广告")
}
}else {
GADMobileAds.sharedInstance().applicationMuted = false
GADMobileAds.sharedInstance().applicationVolume = 0.8
print("没有播放,不需要静音广告")
}
}
}
///广
func isAdSounded() {
guard isMuted == true else {
return
}
DispatchQueue.main.async {
if GADMobileAds.sharedInstance().applicationMuted == true {
GADMobileAds.sharedInstance().applicationMuted = false
GADMobileAds.sharedInstance().applicationVolume = 0.8
print("加载广告时恢复广告声音配置")
}
}
}
//广
func start() {
MP_AdMobManager.shared.start()
MP_AppLovinManager.shared.startConfig()
}
//
@objc private func netWorkReachableAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
guard openAdStatus, internalAdStatus else {return}
if platform {
MP_AppLovinManager.shared.loadMoreAds()
}else {
MP_AdMobManager.shared.loadMoreAdMobs()
}
}
}
//广
func loadMoreAds() {
guard openAdStatus, internalAdStatus else {return}
if platform {
MP_AppLovinManager.shared.loadMoreAds()
}else {
MP_AdMobManager.shared.loadMoreAdMobs()
}
}
///广
func showOpenAdIfAvailable(_ type:OpenType, completion:((_ ad:AnyObject, _ isOpen:Bool, _ platform:Bool) -> Void)?) {
guard openAdStatus, internalAdStatus else {
return
}
isAdMuted()
if platform {
if let block = completion {
MP_AppLovinManager.shared.showOpenAdIfAvailable {[weak self] ad in
guard let self = self else {return}
block(ad, false, true)
}
}else {
MP_AppLovinManager.shared.showOpenAdIfAvailable(nil)
}
}else {
if let block = completion {
MP_AdMobManager.shared.showOpenAdIfAvailable(type) { [weak self] ad, isOpen in
guard let self = self else {return}
block(ad, isOpen, false)
}
}else {
MP_AdMobManager.shared.showOpenAdIfAvailable(type, completion: nil)
}
}
}
///广
func showSearchInterstitialAdIfAvailable(completion:((AnyObject, Bool) -> Void)?) {
guard openAdStatus, internalAdStatus else {return}
isAdMuted()
if platform {
if let block = completion {
MP_AppLovinManager.shared.showSearchInterstitialAdIfAvailable { ad in
block(ad, true)
}
}else {
MP_AppLovinManager.shared.showSearchInterstitialAdIfAvailable(nil)
}
}else {
if let block = completion {
MP_AdMobManager.shared.showSearchInterstitialAdIfAvailable { ad in
block(ad, false)
}
}else {
MP_AdMobManager.shared.showSearchInterstitialAdIfAvailable(completion: nil)
}
}
}
///广
func showPlayInterstitialAdIfAvailable(_ completion:((AnyObject?, Bool) -> Void)?) {
guard openAdStatus, internalAdStatus else {
completion?(nil, false)
return
}
isAdMuted()
if platform {
if let block = completion {
MP_AppLovinManager.shared.showPlayInterstitialAdIfAvailable { ad in
block(ad, true)
}
}else {
MP_AppLovinManager.shared.showPlayInterstitialAdIfAvailable(nil)
}
}else {
if let block = completion {
MP_AdMobManager.shared.showPlayInterstitialAdIfAvailable { ad in
block(ad, false)
}
}else {
MP_AdMobManager.shared.showPlayInterstitialAdIfAvailable(completion: nil)
}
}
}
///广
func showLibraryInterstitialAdIfAvailable(completion:((AnyObject) -> Void)?) {
guard openAdStatus, internalAdStatus else {return}
isAdMuted()
if platform {
if let block = completion {
MP_AppLovinManager.shared.showLibraryInterstitialAdIfAvailable { ad in
block(ad)
}
}else {
MP_AppLovinManager.shared.showLibraryInterstitialAdIfAvailable(nil)
}
}else {
if let block = completion {
MP_AdMobManager.shared.showLibraryInterstitialAdIfAvailable { ad in
block(ad)
}
}else {
MP_AdMobManager.shared.showLibraryInterstitialAdIfAvailable(completion: nil)
}
}
}
}