|
| 1 | +/* Copyright 2018-2024 Prebid.org, Inc. |
| 2 | + |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import GoogleMobileAds |
| 17 | + |
| 18 | +final class PrebidUniversalCreativeTestingGAMController: |
| 19 | + NSObject, |
| 20 | + AdaptedController, |
| 21 | + PrebidConfigurableBannerController, |
| 22 | + GADBannerViewDelegate, WKNavigationDelegate { |
| 23 | + |
| 24 | + var refreshInterval: TimeInterval = 0 |
| 25 | + var prebidConfigId: String = "" |
| 26 | + var gamAdUnitID: String = "" |
| 27 | + var adSize = CGSize.zero |
| 28 | + |
| 29 | + let rootController: AdapterViewController |
| 30 | + |
| 31 | + private var gamBanner: GAMBannerView! |
| 32 | + |
| 33 | + private let configIdLabel = UILabel() |
| 34 | + private let reloadButton = ThreadCheckingButton() |
| 35 | + |
| 36 | + init(rootController: AdapterViewController) { |
| 37 | + self.rootController = rootController |
| 38 | + super.init() |
| 39 | + |
| 40 | + setupAdapterController() |
| 41 | + } |
| 42 | + |
| 43 | + func configurationController() -> BaseConfigurationController? { |
| 44 | + return PrebidBannerConfigurationController(controller: self) |
| 45 | + } |
| 46 | + |
| 47 | + private func setupAdapterController() { |
| 48 | + rootController.showButton.isHidden = true |
| 49 | + |
| 50 | + configIdLabel.isHidden = true |
| 51 | + rootController.actionsView.addArrangedSubview(configIdLabel) |
| 52 | + |
| 53 | + reloadButton.addTarget(self, action: #selector(reload), for: .touchUpInside) |
| 54 | + } |
| 55 | + |
| 56 | + func loadAd() { |
| 57 | + configIdLabel.isHidden = false |
| 58 | + configIdLabel.text = "GAM AdUnit ID: \(gamAdUnitID)" |
| 59 | + |
| 60 | + gamBanner = GAMBannerView(adSize: GADAdSizeFromCGSize(adSize)) |
| 61 | + gamBanner.adUnitID = gamAdUnitID |
| 62 | + gamBanner.rootViewController = rootController |
| 63 | + gamBanner.delegate = self |
| 64 | + |
| 65 | + rootController.bannerView?.addSubview(gamBanner) |
| 66 | + |
| 67 | + let gamRequest = GAMRequest() |
| 68 | + gamBanner.load(gamRequest) |
| 69 | + } |
| 70 | + |
| 71 | + @objc private func reload() { |
| 72 | + reloadButton.isEnabled = false |
| 73 | + let gamRequest = GAMRequest() |
| 74 | + gamBanner.load(gamRequest) |
| 75 | + } |
| 76 | + |
| 77 | + func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { |
| 78 | + rootController.bannerView.backgroundColor = .clear |
| 79 | + reloadButton.isEnabled = true |
| 80 | + rootController.bannerView.constraints.first { $0.firstAttribute == .width }?.constant = 300 |
| 81 | + rootController.bannerView.constraints.first { $0.firstAttribute == .height }?.constant = 500 |
| 82 | + |
| 83 | + let targetWebView = bannerView.allSubViewsOf(type: WKWebView.self).first |
| 84 | + |
| 85 | + if #available(iOS 14.0, *) { |
| 86 | + targetWebView?.configuration.defaultWebpagePreferences.allowsContentJavaScript = true |
| 87 | + } else { |
| 88 | + targetWebView?.configuration.preferences.javaScriptEnabled = true |
| 89 | + } |
| 90 | + |
| 91 | + targetWebView?.navigationDelegate = self |
| 92 | + |
| 93 | +// let js = """ |
| 94 | +// function loadNewIframe() { |
| 95 | +// // The URL to load in the iframe |
| 96 | +// const iframeUrl = "http://192.168.0.102:9876"; // You can change this dynamically |
| 97 | +// |
| 98 | +// // Create the iframe element |
| 99 | +// const iframe = document.createElement("iframe"); |
| 100 | +// iframe.src = iframeUrl; |
| 101 | +// |
| 102 | +// // Optional: Set other attributes like width, height, and frameBorder |
| 103 | +// iframe.width = "100%"; |
| 104 | +// iframe.height = "400px"; |
| 105 | +// iframe.frameBorder = "0"; // Removes the border of the iframe |
| 106 | +// |
| 107 | +// // Append the iframe to the end of the body |
| 108 | +// document.body.appendChild(iframe); |
| 109 | +// } |
| 110 | +// |
| 111 | +// // Call the function to load the iframe (you can bind this to a button or another event) |
| 112 | +// loadNewIframe(); |
| 113 | +// """ |
| 114 | +// |
| 115 | +// targetWebView?.evaluateJavaScript(js) |
| 116 | + |
| 117 | +// targetWebView?.load(URLRequest(url: URL(string: "http://192.168.0.102:9876")!)) |
| 118 | + } |
| 119 | + |
| 120 | + func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: any Error) { |
| 121 | + print(error.localizedDescription) |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments