Skip to content

Commit

Permalink
Feat/add_late_binding_native_demo (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy-Hu authored Mar 14, 2022
1 parent eaeb17c commit e49410b
Show file tree
Hide file tree
Showing 12 changed files with 687 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
5BCB21DB27471AB7007218B2 /* ALDemoProgrammaticMRecViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BCB21DA27471AB7007218B2 /* ALDemoProgrammaticMRecViewController.m */; };
5BCB21DD274721F0007218B2 /* MRecs.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5BCB21DC274721F0007218B2 /* MRecs.storyboard */; };
5BCB21E8274C6459007218B2 /* ALDemoInterfaceBuilderMRecViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BCB21E7274C6459007218B2 /* ALDemoInterfaceBuilderMRecViewController.m */; };
77B6F431279B51850063F81D /* NativeCustomAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77B6F430279B51850063F81D /* NativeCustomAdView.xib */; };
77AAC0F527D800890060499A /* ALMAXManualNativeLateBindingAdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77AAC0F427D800890060499A /* ALMAXManualNativeLateBindingAdViewController.m */; };
77B6F431279B51850063F81D /* NativeManualAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77B6F430279B51850063F81D /* NativeManualAdView.xib */; };
77B6F437279B51AB0063F81D /* ALMAXManualNativeAdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B6F433279B51AB0063F81D /* ALMAXManualNativeAdViewController.m */; };
77B6F438279B51AB0063F81D /* ALMAXTemplateNativeAdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B6F436279B51AB0063F81D /* ALMAXTemplateNativeAdViewController.m */; };
C0DE8BB3234E8A86004B0CFC /* ALBaseAdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C0DE8BB2234E8A86004B0CFC /* ALBaseAdViewController.m */; };
Expand Down Expand Up @@ -77,7 +78,9 @@
5BCB21DC274721F0007218B2 /* MRecs.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = MRecs.storyboard; sourceTree = "<group>"; };
5BCB21E6274C6459007218B2 /* ALDemoInterfaceBuilderMRecViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ALDemoInterfaceBuilderMRecViewController.h; sourceTree = "<group>"; };
5BCB21E7274C6459007218B2 /* ALDemoInterfaceBuilderMRecViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALDemoInterfaceBuilderMRecViewController.m; sourceTree = "<group>"; };
77B6F430279B51850063F81D /* NativeCustomAdView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NativeCustomAdView.xib; sourceTree = "<group>"; };
77AAC0F327D800500060499A /* ALMAXManualNativeLateBindingAdViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ALMAXManualNativeLateBindingAdViewController.h; sourceTree = "<group>"; };
77AAC0F427D800890060499A /* ALMAXManualNativeLateBindingAdViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALMAXManualNativeLateBindingAdViewController.m; sourceTree = "<group>"; };
77B6F430279B51850063F81D /* NativeManualAdView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NativeManualAdView.xib; sourceTree = "<group>"; };
77B6F433279B51AB0063F81D /* ALMAXManualNativeAdViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ALMAXManualNativeAdViewController.m; sourceTree = "<group>"; };
77B6F434279B51AB0063F81D /* ALMAXTemplateNativeAdViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALMAXTemplateNativeAdViewController.h; sourceTree = "<group>"; };
77B6F435279B51AB0063F81D /* ALMAXManualNativeAdViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALMAXManualNativeAdViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,7 +212,7 @@
1D993015231FA39000C472F8 /* Supporting Files */ = {
isa = PBXGroup;
children = (
77B6F430279B51850063F81D /* NativeCustomAdView.xib */,
77B6F430279B51850063F81D /* NativeManualAdView.xib */,
1D992FED231FA1C500C472F8 /* main.m */,
1D992FEC231FA1C500C472F8 /* Info.plist */,
1D992FE7231FA1C500C472F8 /* Assets.xcassets */,
Expand Down Expand Up @@ -269,6 +272,8 @@
children = (
77B6F435279B51AB0063F81D /* ALMAXManualNativeAdViewController.h */,
77B6F433279B51AB0063F81D /* ALMAXManualNativeAdViewController.m */,
77AAC0F327D800500060499A /* ALMAXManualNativeLateBindingAdViewController.h */,
77AAC0F427D800890060499A /* ALMAXManualNativeLateBindingAdViewController.m */,
77B6F434279B51AB0063F81D /* ALMAXTemplateNativeAdViewController.h */,
77B6F436279B51AB0063F81D /* ALMAXTemplateNativeAdViewController.m */,
);
Expand Down Expand Up @@ -486,7 +491,7 @@
5BCB21DD274721F0007218B2 /* MRecs.storyboard in Resources */,
E56785F423F35B8F00ACA6C1 /* Leaders.storyboard in Resources */,
1D992FE8231FA1C500C472F8 /* Assets.xcassets in Resources */,
77B6F431279B51850063F81D /* NativeCustomAdView.xib in Resources */,
77B6F431279B51850063F81D /* NativeManualAdView.xib in Resources */,
E56784A623F22AB300ACA6C1 /* Rewarded.storyboard in Resources */,
E57306F123EB90D500D972F4 /* Interstitials.storyboard in Resources */,
E567854B23F3541100ACA6C1 /* Banners.storyboard in Resources */,
Expand Down Expand Up @@ -523,6 +528,7 @@
37C7E1EC2328904E002165B5 /* ALMAXFrameLayoutBannerAdViewController.m in Sources */,
E573083423EB970400D972F4 /* ALDemoInterstitialManualLoadingViewController.m in Sources */,
37C7E1ED2328904E002165B5 /* ALMAXInterstitialAdViewController.m in Sources */,
77AAC0F527D800890060499A /* ALMAXManualNativeLateBindingAdViewController.m in Sources */,
37C7E1EE2328904E002165B5 /* ALMAXRewardedAdViewController.m in Sources */,
E56785F923F35BC200ACA6C1 /* ALDemoInterfaceBuilderLeaderViewController.m in Sources */,
E573083123EB96E400D972F4 /* ALDemoInterstitialBasicIntegrationViewController.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)viewDidLoad
{
[super viewDidLoad];

UINib *nativeAdViewNib = [UINib nibWithNibName: @"NativeCustomAdView" bundle: NSBundle.mainBundle];
UINib *nativeAdViewNib = [UINib nibWithNibName: @"NativeManualAdView" bundle: NSBundle.mainBundle];
self.nativeAdView = [nativeAdViewNib instantiateWithOwner: nil options: nil].firstObject;

MANativeAdViewBinder *binder = [[MANativeAdViewBinder alloc] initWithBuilderBlock:^(MANativeAdViewBinderBuilder *builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ALMAXManualNativeLateBindingAdViewController.h
// AppLovin MAX Demo App - ObjC
//
// Created by Billy Hu on 3/8/22.
// Copyright © 2022 AppLovin Corporation. All rights reserved.
//

#import "ALBaseAdViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface ALMAXManualNativeLateBindingAdViewController : ALBaseAdViewController

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// ALMAXManualNativeLateBindingAdViewController.m
// AppLovin MAX Demo App - ObjC
//
// Created by Billy Hu on 3/8/22.
// Copyright © 2022 AppLovin Corporation. All rights reserved.
//

#import "ALMAXManualNativeLateBindingAdViewController.h"
#import <Adjust/Adjust.h>
#import <AppLovinSDK/AppLovinSDK.h>

@interface ALMAXManualNativeLateBindingAdViewController()<MANativeAdDelegate, MAAdRevenueDelegate>

@property (nonatomic, weak) IBOutlet UIView *nativeAdContainerView;
@property (nonatomic, weak) IBOutlet UIButton *showAdButton;

@property (nonatomic, strong) MANativeAdLoader *nativeAdLoader;
@property (nonatomic, strong) MANativeAdView *nativeAdView;
@property (nonatomic, strong, nullable) MAAd *nativeAd;

@end

@implementation ALMAXManualNativeLateBindingAdViewController

#pragma mark - View Lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

self.nativeAdLoader = [[MANativeAdLoader alloc] initWithAdUnitIdentifier: @"YOUR_AD_UNIT"];
self.nativeAdLoader.nativeAdDelegate = self;
self.nativeAdLoader.revenueDelegate = self;
}

- (void)dealloc
{
[self cleanUpAdIfNeeded];

self.nativeAdLoader.nativeAdDelegate = nil;
self.nativeAdLoader.revenueDelegate = nil;
}

- (void)cleanUpAdIfNeeded
{
// Clean up any pre-existing native ad to prevent memory leaks
if ( self.nativeAd )
{
[self.nativeAdLoader destroyAd: self.nativeAd];
}

if ( self.nativeAdView )
{
[self.nativeAdView removeFromSuperview];
}
}

- (MANativeAdView *)createNativeAdView
{
UINib *nativeAdViewNib = [UINib nibWithNibName: @"NativeManualAdView" bundle: NSBundle.mainBundle];
MANativeAdView *nativeAdView = [nativeAdViewNib instantiateWithOwner: nil options: nil].firstObject;

MANativeAdViewBinder *binder = [[MANativeAdViewBinder alloc] initWithBuilderBlock:^(MANativeAdViewBinderBuilder *builder) {
builder.titleLabelTag = 1001;
builder.advertiserLabelTag = 1002;
builder.bodyLabelTag = 1003;
builder.iconImageViewTag = 1004;
builder.optionsContentViewTag = 1005;
builder.mediaContentViewTag = 1006;
builder.callToActionButtonTag = 1007;
}];
[nativeAdView bindViewsWithAdViewBinder: binder];

return nativeAdView;
}

#pragma mark - IB Actions

- (IBAction)loadAd
{
[self cleanUpAdIfNeeded];

[self.nativeAdLoader loadAd];
}

- (IBAction)showAd
{
self.nativeAdView = [self createNativeAdView];
[self.nativeAdLoader renderNativeAdView: self.nativeAdView withAd: self.nativeAd];
[self.nativeAdContainerView addSubview: self.nativeAdView];

[self.showAdButton setEnabled: NO];
}

#pragma mark - NativeAdDelegate Protocol

- (void)didLoadNativeAd:(MANativeAdView *)nativeAdView forAd:(MAAd *)ad
{
[self logCallback: __PRETTY_FUNCTION__];

// Save ad to be rendered later
self.nativeAd = ad;

[self.showAdButton setEnabled: YES];

// Set to false if modifying constraints after adding the ad view to your layout
self.nativeAdContainerView.translatesAutoresizingMaskIntoConstraints = NO;

// Set ad view to span width and height of container and center the ad
[self.nativeAdContainerView.widthAnchor constraintEqualToAnchor: nativeAdView.widthAnchor].active = YES;
[self.nativeAdContainerView.heightAnchor constraintEqualToAnchor: nativeAdView.heightAnchor].active = YES;
[self.nativeAdContainerView.centerXAnchor constraintEqualToAnchor: nativeAdView.centerXAnchor].active = YES;
[self.nativeAdContainerView.centerYAnchor constraintEqualToAnchor: nativeAdView.centerYAnchor].active = YES;
}

- (void)didFailToLoadNativeAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error
{
[self logCallback: __PRETTY_FUNCTION__];
}

- (void)didClickNativeAd:(MAAd *)ad
{
[self logCallback: __PRETTY_FUNCTION__];
}

#pragma mark - MAAdRevenueDelegate Protocol

- (void)didPayRevenueForAd:(MAAd *)ad
{
[self logCallback: __PRETTY_FUNCTION__];

ADJAdRevenue *adjustAdRevenue = [[ADJAdRevenue alloc] initWithSource: ADJAdRevenueSourceAppLovinMAX];
[adjustAdRevenue setRevenue: ad.revenue currency: @"USD"];
[adjustAdRevenue setAdRevenueNetwork: ad.networkName];
[adjustAdRevenue setAdRevenueUnit: ad.adUnitIdentifier];
[adjustAdRevenue setAdRevenuePlacement: ad.placement];

[Adjust trackAdRevenue: adjustAdRevenue];
}

@end
Loading

0 comments on commit e49410b

Please sign in to comment.