2023-03-12 16:29:03 -06:00
|
|
|
#if USE_GOOGLE_IMA
|
2023-12-07 00:47:40 -07:00
|
|
|
import Foundation
|
|
|
|
import GoogleInteractiveMediaAds
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
class RCTIMAAdsManager: NSObject, IMAAdsLoaderDelegate, IMAAdsManagerDelegate, IMALinkOpenerDelegate {
|
|
|
|
private weak var _video: RCTVideo?
|
|
|
|
private var _pipEnabled: () -> Bool
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
/* Entry point for the SDK. Used to make ad requests. */
|
|
|
|
private var adsLoader: IMAAdsLoader!
|
|
|
|
/* Main point of interaction with the SDK. Created by the SDK as the result of an ad request. */
|
|
|
|
private var adsManager: IMAAdsManager!
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
init(video: RCTVideo!, pipEnabled: @escaping () -> Bool) {
|
|
|
|
_video = video
|
|
|
|
_pipEnabled = pipEnabled
|
2022-11-17 04:12:50 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
super.init()
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func setUpAdsLoader() {
|
2024-08-29 04:30:05 -06:00
|
|
|
guard let _video else { return }
|
|
|
|
let settings = IMASettings()
|
|
|
|
if let adLanguage = _video.getAdLanguage() {
|
|
|
|
settings.language = adLanguage
|
|
|
|
}
|
|
|
|
adsLoader = IMAAdsLoader(settings: settings)
|
2023-12-07 00:47:40 -07:00
|
|
|
adsLoader.delegate = self
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func requestAds() {
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let _video else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
// Create ad display container for ad rendering.
|
|
|
|
let adDisplayContainer = IMAAdDisplayContainer(adContainer: _video, viewController: _video.reactViewController())
|
2022-11-17 04:12:50 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
let adTagUrl = _video.getAdTagUrl()
|
|
|
|
let contentPlayhead = _video.getContentPlayhead()
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
if adTagUrl != nil && contentPlayhead != nil {
|
|
|
|
// Create an ad request with our ad tag, display container, and optional user context.
|
|
|
|
let request = IMAAdsRequest(
|
|
|
|
adTagUrl: adTagUrl!,
|
|
|
|
adDisplayContainer: adDisplayContainer,
|
|
|
|
contentPlayhead: contentPlayhead,
|
|
|
|
userContext: nil
|
|
|
|
)
|
2022-11-17 04:12:50 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
adsLoader.requestAds(with: request)
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
}
|
2022-11-17 04:12:50 -07:00
|
|
|
|
2024-05-01 03:31:20 -06:00
|
|
|
func releaseAds() {
|
|
|
|
guard let adsManager else { return }
|
|
|
|
// Destroy AdsManager may be delayed for a few milliseconds
|
|
|
|
// But what we want is it stopped producing sound immediately
|
|
|
|
// Issue found on tvOS 17, or iOS if view detach & STARTED event happen at the same moment
|
|
|
|
adsManager.volume = 0
|
|
|
|
adsManager.pause()
|
|
|
|
adsManager.destroy()
|
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - Getters
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func getAdsLoader() -> IMAAdsLoader? {
|
|
|
|
return adsLoader
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func getAdsManager() -> IMAAdsManager? {
|
|
|
|
return adsManager
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - IMAAdsLoaderDelegate
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsLoader(_: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let _video else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
// Grab the instance of the IMAAdsManager and set yourself as the delegate.
|
|
|
|
adsManager = adsLoadedData.adsManager
|
|
|
|
adsManager?.delegate = self
|
2022-11-17 04:12:50 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// Create ads rendering settings and tell the SDK to use the in-app browser.
|
|
|
|
let adsRenderingSettings = IMAAdsRenderingSettings()
|
|
|
|
adsRenderingSettings.linkOpenerDelegate = self
|
|
|
|
adsRenderingSettings.linkOpenerPresentingController = _video.reactViewController()
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
adsManager.initialize(with: adsRenderingSettings)
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsLoader(_: IMAAdsLoader, failedWith adErrorData: IMAAdLoadingErrorData) {
|
|
|
|
if adErrorData.adError.message != nil {
|
|
|
|
print("Error loading ads: " + adErrorData.adError.message!)
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
_video?.setPaused(false)
|
2022-11-17 04:01:29 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - IMAAdsManagerDelegate
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsManager(_ adsManager: IMAAdsManager, didReceive event: IMAAdEvent) {
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let _video else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
// Mute ad if the main player is muted
|
|
|
|
if _video.isMuted() {
|
|
|
|
adsManager.volume = 0
|
|
|
|
}
|
|
|
|
// Play each ad once it has been loaded
|
|
|
|
if event.type == IMAAdEventType.LOADED {
|
|
|
|
if _pipEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
adsManager.start()
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
if _video.onReceiveAdEvent != nil {
|
|
|
|
let type = convertEventToString(event: event.type)
|
|
|
|
|
|
|
|
if event.adData != nil {
|
|
|
|
_video.onReceiveAdEvent?([
|
|
|
|
"event": type,
|
|
|
|
"data": event.adData ?? [String](),
|
|
|
|
"target": _video.reactTag!,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
_video.onReceiveAdEvent?([
|
|
|
|
"event": type,
|
|
|
|
"target": _video.reactTag!,
|
|
|
|
])
|
|
|
|
}
|
2023-09-17 13:12:46 -06:00
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsManager(_: IMAAdsManager, didReceive error: IMAAdError) {
|
|
|
|
if error.message != nil {
|
|
|
|
print("AdsManager error: " + error.message!)
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let _video else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
if _video.onReceiveAdEvent != nil {
|
2023-11-27 13:43:30 -07:00
|
|
|
_video.onReceiveAdEvent?([
|
2023-12-07 00:47:40 -07:00
|
|
|
"event": "ERROR",
|
|
|
|
"data": [
|
|
|
|
"message": error.message ?? "",
|
|
|
|
"code": error.code,
|
|
|
|
"type": error.type,
|
|
|
|
],
|
|
|
|
"target": _video.reactTag!,
|
|
|
|
])
|
2023-11-27 13:43:30 -07:00
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// Fall back to playing content
|
|
|
|
_video.setPaused(false)
|
2022-11-17 04:01:29 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsManagerDidRequestContentPause(_: IMAAdsManager) {
|
|
|
|
// Pause the content for the SDK to play ads.
|
|
|
|
_video?.setPaused(true)
|
|
|
|
_video?.setAdPlaying(true)
|
2023-12-02 05:52:01 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func adsManagerDidRequestContentResume(_: IMAAdsManager) {
|
|
|
|
// Resume the content since the SDK is done playing ads (at least for now).
|
|
|
|
_video?.setAdPlaying(false)
|
|
|
|
_video?.setPaused(false)
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - IMALinkOpenerDelegate
|
2023-10-06 10:43:35 -06:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func linkOpenerDidClose(inAppLink _: NSObject) {
|
|
|
|
adsManager?.resume()
|
|
|
|
}
|
2023-10-06 10:43:35 -06:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - Helpers
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
func convertEventToString(event: IMAAdEventType!) -> String {
|
|
|
|
var result = "UNKNOWN"
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
switch event {
|
2022-11-17 04:01:29 -07:00
|
|
|
case .AD_BREAK_READY:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "AD_BREAK_READY"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .AD_BREAK_ENDED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "AD_BREAK_ENDED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .AD_BREAK_STARTED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "AD_BREAK_STARTED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .AD_PERIOD_ENDED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "AD_PERIOD_ENDED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .AD_PERIOD_STARTED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "AD_PERIOD_STARTED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .ALL_ADS_COMPLETED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "ALL_ADS_COMPLETED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .CLICKED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "CLICK"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .COMPLETE:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "COMPLETED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .CUEPOINTS_CHANGED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "CUEPOINTS_CHANGED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .FIRST_QUARTILE:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "FIRST_QUARTILE"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .LOADED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "LOADED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .LOG:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "LOG"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .MIDPOINT:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "MIDPOINT"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .PAUSE:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "PAUSED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .RESUME:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "RESUMED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .SKIPPED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "SKIPPED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .STARTED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "STARTED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .STREAM_LOADED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "STREAM_LOADED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .TAPPED:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "TAPPED"
|
2022-11-17 04:01:29 -07:00
|
|
|
case .THIRD_QUARTILE:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "THIRD_QUARTILE"
|
2022-11-17 04:01:29 -07:00
|
|
|
default:
|
2023-12-07 00:47:40 -07:00
|
|
|
result = "UNKNOWN"
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
return result
|
|
|
|
}
|
2022-11-17 04:01:29 -07:00
|
|
|
}
|
2023-03-12 16:29:03 -06:00
|
|
|
#endif
|