Skip to content

Commit

Permalink
Merge pull request #106 from thekorn/105-fix-adbanner-onadloaded-even…
Browse files Browse the repository at this point in the history
…ts-are-not-fired-on-ios

fix: Enhance iOS ad handling and logging in Xandr module - WIP
  • Loading branch information
thekorn authored Jul 25, 2024
2 parents 14b46ad + 667e0ad commit 9d40b06
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/xandr/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class _XandrExampleState extends State<XandrExample> {
late final XandrController _controller;
final ScrollController _scrollController = ScrollController();
final StreamController<ScrollPosition> _checkIfAdIsInViewport =
StreamController();
StreamController.broadcast();

@override
void dispose() {
Expand Down
33 changes: 33 additions & 0 deletions packages/xandr_ios/ios/Classes/models/FlutterState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
// Created by markus korn on 13.05.24.
//

import AppNexusSDK
import Flutter
import Foundation

private func logResult(_ result: Any?) {
logger
.debug(
message: "flutter api: we got a result back from the flutter api \(String(describing: result))"
)
}

public class FlutterState {
public var isInitialized: Completer<Bool> = .init()
private var flutterAPI: XandrFlutterApi?
Expand Down Expand Up @@ -63,4 +71,29 @@ public class FlutterState {
throw XandrPluginError.runtimeError("Unable to find Banner for widgetId=\(id)")
}
}

public func onAdLoadedAPI(viewId: Int64, width: CGFloat, height: CGFloat, creativeId: String,
adType: ANAdType, tagId: String, auctionId: String, cpm: Double,
memberId: Int) {
let adTypeIndentifier = switch adType {
case ANAdType.banner: "banner"
case ANAdType.native: "native"
case ANAdType.unknown: "unknown"
case ANAdType.video: "video"
@unknown default: "unknwon"
}

flutterAPI?.onAdLoaded(
viewId: viewId,
width: Int64(width),
height: Int64(height),
creativeId: creativeId,
adType: adTypeIndentifier,
tagId: tagId,
auctionId: auctionId,
cpm: cpm,
memberId: Int64(memberId),
completion: logResult
)
}
}
34 changes: 33 additions & 1 deletion packages/xandr_ios/ios/Classes/models/ads/BannerAd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import Flutter
import Foundation

class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate {
private var banner: ANBannerAdView?
public var banner: ANBannerAdView?
public var state: FlutterState?
public var viewId: Int64

init(state: FlutterState, frame: CGRect,
viewIdentifier viewId: Int64,
args: Any?,
binaryMessenger messenger: FlutterBinaryMessenger?) {
self.viewId = viewId
super.init()
// Do any additional setup after loading the view.
ANSDKSettings.sharedInstance().enableOMIDOptimization = true
logger.debug(message: "init banner")
self.state = state

guard let arguments = args as? [String: Any] else {
return
Expand All @@ -45,6 +49,7 @@ class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate {
inventoryCode: inventoryCode!
)
banner?.delegate = self
// banner?.appEventDelegate = self
customKeywords?.forEach { item in
for value in item.value {
banner?.addCustomKeyword(withKey: item.key, value: value)
Expand All @@ -71,4 +76,31 @@ class XandrBanner: NSObject, FlutterPlatformView, ANBannerAdViewDelegate {
func loadAd() {
banner?.loadAd()
}

public func adDidReceiveAd(_ ad: Any) {
if ad is ANBannerAdView {
let a = ad as? ANBannerAdView
if let info = a?.adResponseInfo {
state?.onAdLoadedAPI(
viewId: viewId,
width: a!.loadedAdSize.width,
height: (a?.loadedAdSize.height)!,
creativeId: info.creativeId!,
adType: info.adType,
tagId: "",
auctionId: info.auctionId!,
cpm: info.cpm!.doubleValue,
memberId: info.memberId
)

} else {
logger
.error(
message: "BannerAd.adDidRecieveAd: we did not get an adResponseInfo back, got \(String(describing: a))"
)
}
} else {
logger.error(message: "BannerAd.adDidRecieveAd: recieved an unknown payload \(ad)")
}
}
}

0 comments on commit 9d40b06

Please sign in to comment.