-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://app.asana.com/0/573104092700345/1203788313801344/f https://user-images.githubusercontent.com/27931661/215855424-e1c787a1-1267-4c29-ba01-31cd6ba1e71d.MP4 https://user-images.githubusercontent.com/27931661/215855406-ed928ea8-91d6-4651-b90b-6727094b702f.MP4 --------- Co-authored-by: Harry Arakkal <[email protected]>
- Loading branch information
1 parent
b134d46
commit 7239027
Showing
7 changed files
with
498 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
... Demo App - Swift/AppLovin MAX Demo App - Swift/Base Classes/MAAdViewSwiftUIWrapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// MAAdViewSwiftUIWrapper.swift | ||
// AppLovin MAX Demo App - Swift | ||
// | ||
// Created by Wootae Jeon on 1/26/23. | ||
// Copyright © 2023 AppLovin. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import AppLovinSDK | ||
|
||
@available(iOS 13.0, *) | ||
struct MAAdViewSwiftUIWrapper: UIViewRepresentable | ||
{ | ||
let adUnitIdentifier: String | ||
let adFormat: MAAdFormat | ||
let sdk: ALSdk | ||
|
||
// MAAdViewAdDelegate methods | ||
var didLoad: ((MAAd) -> Void)? = nil | ||
var didFailToLoadAd: ((String, MAError) -> Void)? = nil | ||
var didDisplay: ((MAAd) -> Void)? = nil | ||
var didFailToDisplayAd: ((MAAd, MAError) -> Void)? = nil | ||
var didClick: ((MAAd) -> Void)? = nil | ||
var didExpand: ((MAAd) -> Void)? = nil | ||
var didCollapse: ((MAAd) -> Void)? = nil | ||
var didHide: ((MAAd) -> Void)? = nil | ||
|
||
// MAAdRequestDelegate method | ||
var didStartAdRequest: ((String) -> Void)? = nil | ||
|
||
// MAAdRevenueDelegate method | ||
var didPayRevenue: ((MAAd) -> Void)? = nil | ||
|
||
func makeUIView(context: Context) -> MAAdView | ||
{ | ||
let adView = MAAdView(adUnitIdentifier: adUnitIdentifier, adFormat: adFormat, sdk: sdk) | ||
|
||
adView.delegate = context.coordinator | ||
adView.requestDelegate = context.coordinator | ||
adView.revenueDelegate = context.coordinator | ||
|
||
// Set background or background color for AdViews to be fully functional | ||
adView.backgroundColor = .black | ||
|
||
// Load the first ad | ||
adView.loadAd() | ||
|
||
return adView | ||
} | ||
|
||
func updateUIView(_ uiView: MAAdView, context: Context) {} | ||
|
||
func makeCoordinator() -> Coordinator | ||
{ | ||
Coordinator(parent: self) | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MAAdViewSwiftUIWrapper | ||
{ | ||
class Coordinator: NSObject, MAAdViewAdDelegate, MAAdRequestDelegate, MAAdRevenueDelegate | ||
{ | ||
private let parent: MAAdViewSwiftUIWrapper | ||
|
||
init(parent: MAAdViewSwiftUIWrapper) | ||
{ | ||
self.parent = parent | ||
} | ||
|
||
func didStartAdRequest(forAdUnitIdentifier adUnitIdentifier: String) | ||
{ | ||
parent.didStartAdRequest?(adUnitIdentifier) | ||
} | ||
|
||
func didLoad(_ ad: MAAd) | ||
{ | ||
parent.didLoad?(ad) | ||
} | ||
|
||
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) | ||
{ | ||
parent.didFailToLoadAd?(adUnitIdentifier, error) | ||
} | ||
|
||
func didDisplay(_ ad: MAAd) | ||
{ | ||
parent.didDisplay?(ad) | ||
} | ||
|
||
func didFail(toDisplay ad: MAAd, withError error: MAError) | ||
{ | ||
parent.didFailToDisplayAd?(ad, error) | ||
} | ||
|
||
func didClick(_ ad: MAAd) | ||
{ | ||
parent.didClick?(ad) | ||
} | ||
|
||
func didExpand(_ ad: MAAd) | ||
{ | ||
parent.didExpand?(ad) | ||
} | ||
|
||
func didCollapse(_ ad: MAAd) | ||
{ | ||
parent.didCollapse?(ad) | ||
} | ||
|
||
func didHide(_ ad: MAAd) | ||
{ | ||
parent.didHide?(ad) | ||
} | ||
|
||
func didPayRevenue(for ad: MAAd) | ||
{ | ||
parent.didPayRevenue?(ad) | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MAAdViewSwiftUIWrapper | ||
{ | ||
func deviceSpecificFrame() -> some View | ||
{ | ||
modifier(MAAdViewFrame(adFormat: adFormat)) | ||
} | ||
} | ||
|
||
@available(iOS 13.0.0, *) | ||
struct MAAdViewFrame: ViewModifier | ||
{ | ||
let adFormat: MAAdFormat | ||
|
||
func body(content: Content) -> some View | ||
{ | ||
if ( adFormat == .banner || adFormat == .leader ) | ||
{ | ||
// Stretch to the width of the screen for banners to be fully functional | ||
// Banner height on iPhone and iPad is 50 and 90, respectively | ||
content | ||
.frame(height: (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50) | ||
} | ||
else // adFormat == .mrec | ||
{ | ||
// MREC width and height are 300 and 250 respectively, on iPhone and iPad | ||
content | ||
.frame(width: 300, height: 250) | ||
} | ||
} | ||
} | ||
|
||
struct CallbackTableItem: Identifiable | ||
{ | ||
let id = UUID() | ||
let callback: String | ||
} |
20 changes: 20 additions & 0 deletions
20
... Swift/AppLovin MAX Demo App - Swift/MAX/Banners/ALMAXSwiftUIBannerAdViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// ALMAXSwiftUIBannerAdViewController.swift | ||
// AppLovin MAX Demo App - Swift | ||
// | ||
// Created by Wootae Jeon on 1/26/23. | ||
// Copyright © 2023 AppLovin. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
import AppLovinSDK | ||
|
||
@available(iOS 13.0, *) | ||
class ALMAXSwiftUIBannerAdViewController: UIHostingController<ALMAXSwiftUIBannerAdView> | ||
{ | ||
required init?(coder aDecoder: NSCoder) | ||
{ | ||
super.init(coder: aDecoder, rootView: ALMAXSwiftUIBannerAdView()) | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...o App - Swift/AppLovin MAX Demo App - Swift/MAX/Banners/UI/ALMAXSwiftUIBannerAdView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// ALMAXSwiftUIBannerAdView.swift | ||
// AppLovin MAX Demo App - Swift | ||
// | ||
// Created by Wootae Jeon on 1/26/23. | ||
// Copyright © 2023 AppLovin. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import Adjust | ||
import AppLovinSDK | ||
|
||
@available(iOS 13.0, *) | ||
struct ALMAXSwiftUIBannerAdView: View | ||
{ | ||
@ObservedObject private var viewModel = ALMAXSwiftUIBannerAdViewModel() | ||
|
||
var body: some View { | ||
VStack { | ||
MAAdViewSwiftUIWrapper(adUnitIdentifier: "YOUR_AD_UNIT_ID", | ||
adFormat: .banner, | ||
sdk: ALSdk.shared()!, | ||
didLoad: viewModel.didLoad, | ||
didFailToLoadAd: viewModel.didFailToLoadAd, | ||
didDisplay: viewModel.didDisplay, | ||
didFailToDisplayAd: viewModel.didFail, | ||
didClick: viewModel.didClick, | ||
didExpand: viewModel.didExpand, | ||
didCollapse: viewModel.didCollapse, | ||
didHide: viewModel.didHide, | ||
didPayRevenue: viewModel.didPayRevenue) | ||
.deviceSpecificFrame() | ||
|
||
callbacksTable | ||
.frame(maxHeight: .infinity) | ||
} | ||
} | ||
|
||
var callbacksTable: some View { | ||
List(viewModel.callbacks) { | ||
Text($0.callback) | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
class ALMAXSwiftUIBannerAdViewModel: NSObject, ObservableObject | ||
{ | ||
@Published fileprivate var callbacks: [CallbackTableItem] = [] | ||
|
||
private func logCallback(functionName: String = #function) | ||
{ | ||
DispatchQueue.main.async { | ||
withAnimation { | ||
self.callbacks.append(CallbackTableItem(callback: functionName)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension ALMAXSwiftUIBannerAdViewModel: MAAdViewAdDelegate, MAAdRevenueDelegate | ||
{ | ||
// MARK: MAAdDelegate Protocol | ||
func didLoad(_ ad: MAAd) { logCallback() } | ||
|
||
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) { logCallback() } | ||
|
||
func didDisplay(_ ad: MAAd) { logCallback() } | ||
|
||
func didHide(_ ad: MAAd) { logCallback() } | ||
|
||
func didClick(_ ad: MAAd) { logCallback() } | ||
|
||
func didFail(toDisplay ad: MAAd, withError error: MAError) { logCallback() } | ||
|
||
// MARK: MAAdViewAdDelegate Protocol | ||
func didExpand(_ ad: MAAd) { logCallback() } | ||
|
||
func didCollapse(_ ad: MAAd) { logCallback() } | ||
|
||
// MARK: MAAdRevenueDelegate Protocol | ||
func didPayRevenue(for ad: MAAd) | ||
{ | ||
logCallback() | ||
|
||
let adjustAdRevenue = ADJAdRevenue(source: ADJAdRevenueSourceAppLovinMAX)! | ||
adjustAdRevenue.setRevenue(ad.revenue, currency: "USD") | ||
adjustAdRevenue.setAdRevenueNetwork(ad.networkName) | ||
adjustAdRevenue.setAdRevenueUnit(ad.adUnitIdentifier) | ||
if let placement = ad.placement | ||
{ | ||
adjustAdRevenue.setAdRevenuePlacement(placement) | ||
} | ||
|
||
Adjust.trackAdRevenue(adjustAdRevenue) | ||
} | ||
} |
Oops, something went wrong.