Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 7 changed files with 498 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
1055D8A32984A18700228C1B /* MAAdViewSwiftUIWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1055D8A22984A18700228C1B /* MAAdViewSwiftUIWrapper.swift */; };
1055D8A52984A19400228C1B /* ALMAXSwiftUIMRecAdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1055D8A42984A19400228C1B /* ALMAXSwiftUIMRecAdViewController.swift */; };
1055D8A72984A1A200228C1B /* ALMAXSwiftUIMRecAdView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1055D8A62984A1A200228C1B /* ALMAXSwiftUIMRecAdView.swift */; };
1055D8AB2984A1BC00228C1B /* ALMAXSwiftUIBannerAdView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1055D8A92984A1BC00228C1B /* ALMAXSwiftUIBannerAdView.swift */; };
1055D8AC2984A1BC00228C1B /* ALMAXSwiftUIBannerAdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1055D8AA2984A1BC00228C1B /* ALMAXSwiftUIBannerAdViewController.swift */; };
1DDCE59D242AE564007EAC8D /* ALMAXFrameLayoutBannerAdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDCE596242AE564007EAC8D /* ALMAXFrameLayoutBannerAdViewController.swift */; };
1DDCE59E242AE564007EAC8D /* ALMAXInterfaceBuilderBannerAdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDCE597242AE564007EAC8D /* ALMAXInterfaceBuilderBannerAdViewController.swift */; };
1DDCE59F242AE564007EAC8D /* ALMAXAutoLayoutBannerAdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDCE598242AE564007EAC8D /* ALMAXAutoLayoutBannerAdViewController.swift */; };
Expand Down Expand Up @@ -52,6 +57,11 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
1055D8A22984A18700228C1B /* MAAdViewSwiftUIWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MAAdViewSwiftUIWrapper.swift; sourceTree = "<group>"; };
1055D8A42984A19400228C1B /* ALMAXSwiftUIMRecAdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXSwiftUIMRecAdViewController.swift; sourceTree = "<group>"; };
1055D8A62984A1A200228C1B /* ALMAXSwiftUIMRecAdView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXSwiftUIMRecAdView.swift; sourceTree = "<group>"; };
1055D8A92984A1BC00228C1B /* ALMAXSwiftUIBannerAdView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXSwiftUIBannerAdView.swift; sourceTree = "<group>"; };
1055D8AA2984A1BC00228C1B /* ALMAXSwiftUIBannerAdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXSwiftUIBannerAdViewController.swift; sourceTree = "<group>"; };
1DDCE596242AE564007EAC8D /* ALMAXFrameLayoutBannerAdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXFrameLayoutBannerAdViewController.swift; sourceTree = "<group>"; };
1DDCE597242AE564007EAC8D /* ALMAXInterfaceBuilderBannerAdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXInterfaceBuilderBannerAdViewController.swift; sourceTree = "<group>"; };
1DDCE598242AE564007EAC8D /* ALMAXAutoLayoutBannerAdViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ALMAXAutoLayoutBannerAdViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -110,12 +120,22 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1055D8A82984A1BC00228C1B /* UI */ = {
isa = PBXGroup;
children = (
1055D8A92984A1BC00228C1B /* ALMAXSwiftUIBannerAdView.swift */,
);
path = UI;
sourceTree = "<group>";
};
1DDCE595242AE564007EAC8D /* Banners */ = {
isa = PBXGroup;
children = (
1055D8A82984A1BC00228C1B /* UI */,
1DDCE598242AE564007EAC8D /* ALMAXAutoLayoutBannerAdViewController.swift */,
1DDCE596242AE564007EAC8D /* ALMAXFrameLayoutBannerAdViewController.swift */,
1DDCE597242AE564007EAC8D /* ALMAXInterfaceBuilderBannerAdViewController.swift */,
1055D8AA2984A1BC00228C1B /* ALMAXSwiftUIBannerAdViewController.swift */,
);
path = Banners;
sourceTree = "<group>";
Expand All @@ -139,9 +159,10 @@
1DDCE5A2242AE5CE007EAC8D /* Base Classes */ = {
isa = PBXGroup;
children = (
1DDCE5A3242AE5CE007EAC8D /* ALHomeViewController.swift */,
1DDCE5A4242AE5CE007EAC8D /* ALBaseAdViewController.swift */,
1DDCE5A3242AE5CE007EAC8D /* ALHomeViewController.swift */,
35C0C88527F79E090030B2F8 /* ALTextCollectionViewCell.swift */,
1055D8A22984A18700228C1B /* MAAdViewSwiftUIWrapper.swift */,
);
path = "Base Classes";
sourceTree = "<group>";
Expand Down Expand Up @@ -213,6 +234,7 @@
37B6CECD246DE9FF0068A6A4 /* ALMAXFrameLayoutMRecAdViewController.swift */,
37B6CECF246DEA300068A6A4 /* ALMAXInterfaceBuilderMRecAdViewController.swift */,
6254AC68286A5741000D3298 /* ALMAXMRecTableViewController.swift */,
1055D8A42984A19400228C1B /* ALMAXSwiftUIMRecAdViewController.swift */,
);
path = MRECs;
sourceTree = "<group>";
Expand All @@ -230,6 +252,7 @@
isa = PBXGroup;
children = (
6254AC6C286A5E90000D3298 /* ALMAXMRecTableViewCell.swift */,
1055D8A62984A1A200228C1B /* ALMAXSwiftUIMRecAdView.swift */,
);
path = UI;
sourceTree = "<group>";
Expand Down Expand Up @@ -389,6 +412,7 @@
E5BDBE8C243404D100F82D79 /* ALDemoInterfaceBuilderLeaderViewController.swift in Sources */,
35C0C88627F79E090030B2F8 /* ALTextCollectionViewCell.swift in Sources */,
77B6F43E279B51F70063F81D /* ALMAXTemplateNativeAdViewController.swift in Sources */,
1055D8A32984A18700228C1B /* MAAdViewSwiftUIWrapper.swift in Sources */,
5BCB21D227445E46007218B2 /* ALDemoMRecProgrammaticViewController.swift in Sources */,
E5BDBE862433F4FF00F82D79 /* ALDemoBannerZoneViewController.swift in Sources */,
E5BDBE8B243404D100F82D79 /* ALDemoProgrammaticLeaderViewController.swift in Sources */,
Expand All @@ -403,18 +427,22 @@
E5BDBDFE2432C4E800F82D79 /* ALDemoRewardedVideosZoneViewController.swift in Sources */,
1DDCE59F242AE564007EAC8D /* ALMAXAutoLayoutBannerAdViewController.swift in Sources */,
1DDCE5A0242AE564007EAC8D /* ALMAXRewardedAdViewController.swift in Sources */,
1055D8A72984A1A200228C1B /* ALMAXSwiftUIMRecAdView.swift in Sources */,
37C4D0E8233D834900096894 /* main.swift in Sources */,
37B6CECE246DE9FF0068A6A4 /* ALMAXFrameLayoutMRecAdViewController.swift in Sources */,
35C0C88227F79C820030B2F8 /* ALMAXAdPlacerTableViewController.swift in Sources */,
1055D8A52984A19400228C1B /* ALMAXSwiftUIMRecAdViewController.swift in Sources */,
77AAC0F727D806820060499A /* ALMAXManualNativeLateBindingAdViewController.swift in Sources */,
1DDCE5A6242AE5CE007EAC8D /* ALBaseAdViewController.swift in Sources */,
77B6F43D279B51F70063F81D /* ALMAXManualNativeAdViewController.swift in Sources */,
1DDCE59D242AE564007EAC8D /* ALMAXFrameLayoutBannerAdViewController.swift in Sources */,
E5BDBDFA2432C4E800F82D79 /* ALDemoInterstitialBasicIntegrationViewController.swift in Sources */,
1055D8AB2984A1BC00228C1B /* ALMAXSwiftUIBannerAdView.swift in Sources */,
3763B23723357C4B00E49783 /* ALAppDelegate.swift in Sources */,
E5BDBE872433F4FF00F82D79 /* ALDemoProgrammaticBannerViewController.swift in Sources */,
E5BDBFF124354C4000F82D79 /* ALEventTrackingViewController.swift in Sources */,
E5BDBDFD2432C4E800F82D79 /* ALDemoRewardedVideosViewController.swift in Sources */,
1055D8AC2984A1BC00228C1B /* ALMAXSwiftUIBannerAdViewController.swift in Sources */,
35C0C88427F79C910030B2F8 /* ALMAXAdPlacerCollectionViewController.swift in Sources */,
E5BDBDFB2432C4E800F82D79 /* ALDemoInterstitialZoneViewController.swift in Sources */,
);
Expand Down
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
}
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())
}
}
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)
}
}
Loading

0 comments on commit 7239027

Please sign in to comment.