手动下载 MAX 的广告平台
要接收版本更新,请订阅 AppLovin-MAX-SDK-iOS GitHub 库。
下载最新 iOS SDK
集成 AppLovin SDK
已下载的 ZIP 文件包含 AppLovinSDK.xcframework
文件。
要将 SDK 添加到您的应用程序,请将 AppLovinSDK.xcframework
文件拖拽至您的 Xcode 项目中,并确保 Xcode 项目目标设置里的 Frameworks, Libraries, and Embedded Content 部分中包含 AppLovinSDK.xcframework
。
启用 Xcode 标记
您必须添加 -ObjC
标记才能编译 AppLovin SDK。
要启用 -ObjC
标记,请选择 File > Project Settings,前往 Build Settings,搜索 Other Linker Flags,然后点击 + 符号以添加 -ObjC
。
添加框架
请在您的项目中关联下列框架:
- AdSupport
- AppTrackingTransparency
- AudioToolbox
- AVFoundation
- CoreGraphics
- CoreMedia
- CoreMotion
- CoreTelephony
- Foundation
- MessageUI
- libz
- SafariServices
- StoreKit
- SystemConfiguration
- UIKit
- WebKit
启用 Ad Review
要启用 MAX Ad Review 服务,再下载AppLovinQualityServiceSetup-ios.rb
并将它移入您的项目文件夹中。
打开终端窗口,将 cd
进入到您的项目目录中,然后运行:
ruby AppLovinQualityServiceSetup-ios.rb
添加 SDK Key
选择 File > Project Settings > Info。
点击 Custom iOS Properties 中的一行,然后点击 + 符号以添加新的行。
将新一行的键设置为 AppLovinSdkKey
,并将值设为您的 SDK Key。
您可以在 AppLovin 控制面板的 Account > General > Keys 部分找到 SDK Key。
初始化 SDK
将以下片段添加至应用委托的 application:applicationDidFinishLaunching:
方法:
#import <AppLovinSDK/AppLovinSDK.h>⋮
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // Set the mediation provider value to @"max" to ensure proper functionality. [ALSdk shared].mediationProvider = @"max"; [[ALSdk shared] initializeSdkWithCompletionHandler:^(ALSdkConfiguration *configuration) { // AppLovin SDK is initialized, start loading ads }];
⋮
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate{ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Set the mediation provider value to "max" to ensure proper functionality. ALSdk.shared().mediationProvider = "max" ALSdk.shared().initializeSdk { (configuration: ALSdkConfiguration) in // AppLovin SDK is initialized, start loading ads }
⋮
SKAdNetwork
请参阅 SKAdNetwork 文档,查看集成说明。
插屏广告
加载插屏广告
要加载插屏广告,请使用您的广告单元创建一个 MAInterstitialAd
对象,并调用 loadAd()
。实现 MAAdDelegate
,以便在广告准备就绪以及发生其他广告事件时收到通知。
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdDelegate>@property (nonatomic, strong) MAInterstitialAd *interstitialAd;@property (nonatomic, assign) NSInteger retryAttempt;@end
@implementation ExampleViewController
- (void)createInterstitialAd{ self.interstitialAd = [[MAInterstitialAd alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»"]; self.interstitialAd.delegate = self;
// Load the first ad [self.interstitialAd loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad{ // Interstitial ad is ready to be shown. '[self.interstitialAd isReady]' will now return 'YES'
// Reset retry attempt self.retryAttempt = 0;}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error{ // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
self.retryAttempt++; NSInteger delaySec = pow(2, MIN(6, self.retryAttempt));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self.interstitialAd loadAd]; });}
- (void)didDisplayAd:(MAAd *)ad {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didHideAd:(MAAd *)ad{ // Interstitial ad is hidden. Pre-load the next ad [self.interstitialAd loadAd];}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error{ // Interstitial ad failed to display. AppLovin recommends that you load the next ad [self.interstitialAd loadAd];}
@end
class ExampleViewController: UIViewController, MAAdDelegate{ var interstitialAd: MAInterstitialAd! var retryAttempt = 0.0
func createInterstitialAd() { interstitialAd = MAInterstitialAd(adUnitIdentifier: "«ad-unit-ID»") interstitialAd.delegate = self
// Load the first ad interstitialAd.load() }
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) { // Interstitial ad is ready to be shown. 'interstitialAd.isReady' will now return 'true'
// Reset retry attempt retryAttempt = 0 }
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) { // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt += 1 let delaySec = pow(2.0, min(6.0, retryAttempt))
DispatchQueue.main.asyncAfter(deadline: .now() + delaySec) { self.interstitialAd.load() } }
func didDisplay(_ ad: MAAd) {}
func didClick(_ ad: MAAd) {}
func didHide(_ ad: MAAd) { // Interstitial ad is hidden. Pre-load the next ad interstitialAd.load() }
func didFail(toDisplay ad: MAAd, withError error: MAError) { // Interstitial ad failed to display. AppLovin recommends that you load the next ad interstitialAd.load() }}
显示插屏广告
要展示插屏广告,请在刚才创建的 MAInterstitialAd
实例对象上调用 showAd()
if ( [self.interstitialAd isReady] ){ [self.interstitialAd showAd];}
if interstitialAd.isReady{interstitialAd.show()}
激励广告
加载激励广告
要加载激励广告,请使用您的激励广告单元调取一个 MARewardedAd
对象,并对其调用 loadAd()
。
安装 MARewardedAdDelegate
,以便在广告准备就绪以及发生其他广告事件时收到通知。
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MARewardedAdDelegate>@property (nonatomic, strong) MARewardedAd *rewardedAd;@property (nonatomic, assign) NSInteger retryAttempt;@end
@implementation ExampleViewController
- (void)createRewardedAd{ self.rewardedAd = [MARewardedAd sharedWithAdUnitIdentifier: @"«ad-unit-ID»"]; self.rewardedAd.delegate = self;
// Load the first ad [self.rewardedAd loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad{ // Rewarded ad is ready to be shown. '[self.rewardedAd isReady]' will now return 'YES'
// Reset retry attempt self.retryAttempt = 0;}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error{ // Rewarded ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
self.retryAttempt++; NSInteger delaySec = pow(2, MIN(6, self.retryAttempt));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self.rewardedAd loadAd]; });}
- (void)didDisplayAd:(MAAd *)ad {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didHideAd:(MAAd *)ad{ // Rewarded ad is hidden. Pre-load the next ad [self.rewardedAd loadAd];}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error{ // Rewarded ad failed to display. AppLovin recommends that you load the next ad [self.rewardedAd loadAd];}
#pragma mark - MARewardedAdDelegate Protocol
- (void)didRewardUserForAd:(MAAd *)ad withReward:(MAReward *)reward{ // Rewarded ad was displayed and user should receive the reward}
@end
class ExampleViewController : UIViewController, MARewardedAdDelegate{ var rewardedAd: MARewardedAd! var retryAttempt = 0.0
func createRewardedAd() { rewardedAd = MARewardedAd.shared(withAdUnitIdentifier: "«ad-unit-ID»") rewardedAd.delegate = self
// Load the first ad rewardedAd.load() }
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) { // Rewarded ad is ready to be shown. '[self.rewardedAd isReady]' will now return 'YES'
// Reset retry attempt retryAttempt = 0 }
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) { // Rewarded ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt += 1 let delaySec = pow(2.0, min(6.0, retryAttempt))
DispatchQueue.main.asyncAfter(deadline: .now() + delaySec) { self.rewardedAd.load() } }
func didDisplay(_ ad: MAAd) {}
func didClick(_ ad: MAAd) {}
func didHide(_ ad: MAAd) { // Rewarded ad is hidden. Pre-load the next ad rewardedAd.load() }
func didFail(toDisplay ad: MAAd, withError error: MAError) { // Rewarded ad failed to display. AppLovin recommends that you load the next ad rewardedAd.load() }
// MARK: MARewardedAdDelegate Protocol
func didRewardUser(for ad: MAAd, with reward: MAReward) { // Rewarded ad was displayed and user should receive the reward }}
显示激励广告
要展示激励广告,请在刚才创建的 MARewardedAd
实例对象上调用 showAd()
if ( [self.rewardedAd isReady] ){ [self.rewardedAd showAd];}
if rewardedAd.isReady{ rewardedAd.show()}
横幅和MREC
加载和展示横幅和 MREC
要加载广告,请使用您的广告单元创建一个 MAAdView
对象,并调用 loadAd()
。要展示广告,请将 MAAdView
对象添加为视图层级的子视图。安装 MAAdViewAdDelegate
,以便在广告准备就绪以及发生其他广告事件时收到通知。
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdViewAdDelegate>@property (nonatomic, strong) MAAdView *adView;@end
@implementation ExampleViewController
- (void)createBannerAd{ self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»"]; self.adView.delegate = self;
// Banner height on iPhone and iPad is 50 and 90, respectively CGFloat height = (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 90 : 50;
// Stretch to the width of the screen for banners to be fully functional CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
self.adView.frame = CGRectMake(x, y, width, height);
// Set background or background color for banner ads to be fully functional self.adView.backgroundColor = BACKGROUND_COLOR;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad {}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}
#pragma mark - MAAdViewAdDelegate Protocol
- (void)didExpandAd:(MAAd *)ad {}
- (void)didCollapseAd:(MAAd *)ad {}
#pragma mark - Deprecated Callbacks
- (void)didDisplayAd:(MAAd *)ad { /* use this for impression tracking */ }- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@end
class ExampleViewController: UIViewController, MAAdViewAdDelegate{ var adView: MAAdView!
func createBannerAd() { adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»") adView.delegate = self
// Banner height on iPhone and iPad is 50 and 90, respectively let height: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional let width: CGFloat = UIScreen.main.bounds.width
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set background or background color for banner ads to be fully functional adView.backgroundColor = BACKGROUND_COLOR
view.addSubview(adView)
// Load the first ad adView.loadAd() }
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didClick(_ ad: MAAd) {}
func didFail(toDisplay ad: MAAd, withError error: MAError) {}
// MARK: MAAdViewAdDelegate Protocol
func didExpand(_ ad: MAAd) {}
func didCollapse(_ ad: MAAd) {}
// MARK: Deprecated Callbacks
func didDisplay(_ ad: MAAd) { /* use this for impression tracking */ } func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdViewAdDelegate>@property (nonatomic, strong) MAAdView *adView;@end
@implementation ExampleViewController
- (void)createMRECAd{ self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»" adlanguage: MAAdFormat.mrec]; self.adView.delegate = self;
// MREC width and height are 300 and 250 respectively, on iPhone and iPad CGFloat width = 300; CGFloat height = 250;
// Center the MREC CGFloat x = self.view.center.x - 150;
self.adView.frame = CGRectMake(x, y, width, height);
// Set background or background color for MREC ads to be fully functional self.adView.backgroundColor = BACKGROUND_COLOR;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad {}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}
#pragma mark - MAAdViewAdDelegate Protocol
- (void)didExpandAd:(MAAd *)ad {}
- (void)didCollapseAd:(MAAd *)ad {}
#pragma mark - Deprecated Callbacks
- (void)didDisplayAd:(MAAd *)ad { /* use this for impression tracking */ }- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@end
class ExampleViewController: UIViewController, MAAdViewAdDelegate{ var adView: MAAdView!
func createMRECAd { adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", adlanguage: MAAdFormat.mrec) adView.delegate = self
// MREC width and height are 300 and 250 respectively, on iPhone and iPad let height: CGFloat = 250 let width: CGFloat = 300
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Center the MREC adView.center.x = view.center.x
// Set background or background color for MREC ads to be fully functional adView.backgroundColor = BACKGROUND_COLOR
view.addSubview(adView)
// Load the first ad adView.loadAd() }
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didClick(_ ad: MAAd) {}
func didFail(toDisplay ad: MAAd, withError error: MAError) {}
// MARK: MAAdViewAdDelegate Protocol
func didExpand(_ ad: MAAd) {}
func didCollapse(_ ad: MAAd) {}
// MARK: Deprecated Callbacks
func didDisplay(_ ad: MAAd) { /* use this for impression tracking */ } func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }}
要隐藏横幅或 MREC,请进行如下调用:
adView.hidden = YES;[adView stopAutoRefresh];
adView.isHidden = trueadView.stopAutoRefresh()
要显示横幅或 MREC,请进行如下调用:
adView.hidden = NO;[adView startAutoRefresh];
adView.isHidden = falseadView.startAutoRefresh()
选择要集成的广告平台
选择要集成的广告平台, 然后按照以下说明操作。