跳至主要內容

开屏广告

大约 4 分钟

开屏广告

概述

开屏广告是应用程序启动时呈现的全屏广告,开发者只需遵循标准接入流程即可展示广告视图。开屏广告在应用启动时显示,持续5秒后自动关闭。用户也可以点击“跳过”快速进入主界面,或点击广告了解更多信息。

SASplashAd 方法和属性介绍

方法说明
- (instancetype)initWithSlotId:(NSString *)slotId;全屏开屏广告构造方法,传入广告位ID
- (instancetype)initWithSlotId:(NSString *)slotId adSize:(CGSize)adSize;半屏开屏广告构造方法,传入广告位ID和广告大小。需要和
- (void)loadAd;加载广告
- (void)showAdInWindow:(UIWindow *)window;展示开屏广告,传入应用当前keyWindow。
- (BOOL)isReady;预先加载的开屏广告在需要展示时可能已经失效,需要调用此方法进行判断。
- (void)loadAdAndPlay;加载并展示,广告在加载完毕后会直接开始展示
- (NSInteger)getECPM;
- (nullableSAAdSourceInfo *)getFillAdSourceInfo;
- (nullableNSArray<SAAdErrorInfo *> *)getAdErrorInfoList;

SASplashAd 的代理方法

/// 开屏广告的回调协议
@protocol SASplashAdDelegate <NSObject>
@optional
/// 开屏广告加载成功
- (void)sa_splashAdLoadSuccess:(SASplashAd *)splashAd;
/// 开屏广告的各种错误信息
- (void)sa_splashAd:(SASplashAd *)splashAd didFailWithError:(NSError *_Nullable)error;
/// 开屏广告即将展示
- (void)sa_splashAdWillShow:(SASplashAd *)splashAd;
/// 开屏广告已经展示
- (void)sa_splashAdDidShow:(SASplashAd *)splashAd;
/// 点击了开屏广告
- (void)sa_splashAdDidClick:(SASplashAd *)splashAd;
/// 开屏广告将要关闭
- (void)sa_splashAdWillClosed:(SASplashAd *)splashAd;
/// 开屏广告已经关闭
- (void)sa_splashAdDidClose:(SASplashAd *)splashAd;
/// 开屏广告点击了跳过
- (void)sa_splashAdDidClickSkip:(SASplashAd *)splashAd;
/// 开屏广告播放完毕
- (void)sa_splashVideoAdDidPlayFinish:(SASplashAd *)splashAd;
/// 开屏广告剩余时间回调
- (void)sa_splashAd:(SASplashAd *)splashAd lifeTime:(NSUInteger)time;
/// 开屏广告曝光回调
- (void)sa_splashAdExposed:(SASplashAd *)splashAd;

开屏广告接入示例

AppDelegate为例:

@import SAAdSDK;

@interface AppDelegate ()<SASplashAdDelegate>

@property (nonatomic, assign) NSTimeInterval enterBackgroundTime;
@property (nonatomic, strong) SASplashAd *splashAd;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[SAAdSDK shared] setupWithConf: conf completion: ^(NSError *error){
        // 初始化成功
        [self loadSplashAdAndShow];
    }];
    
    return YES;
}

- (void)loadSplashAdAndShow {
    // 使用提前申请好的广告位ID
    self.splashAd = [[SASplashAd alloc] initWithSlotId:SADAdConfig.current.splashId];
    self.splashAd.delegate = self;
    [self.splashAd loadAd];
}

// 热启动开屏处理,以进入后台10秒为例
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSTimeInterval now = [NSDate date].timeIntervalSince1970;
    if (self.enterBackgroundTime > 0 && (now - self.enterBackgroundTime) > 10) {
        [self loadSplashAdAndShow];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.enterBackgroundTime = [NSDate date].timeIntervalSince1970;
}

//MARK: - SASplashAdDelegate
- (void)sa_splashAdLoadSuccess:(SASplashAd *)splashAd {
    [splashAd showAdInWindow:UIApplication.sharedApplication.keyWindow];
}
// 略...
@end

如果使用 SceneDelegate 托管应用生命周期,热启动使用如下方法

@import SAAdSDK;

@interface SceneDelegate ()<SASplashAdDelegate>
// 略...
@end

@implementation SceneDelegate

- (void)sceneWillEnterForeground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    NSTimeInterval now = [NSDate date].timeIntervalSince1970;
    if (self.enterBackgroundTime > 0 && (now - self.enterBackgroundTime) > 10) {
        [self loadSplashAdAndShow];
    }
}

- (void)sceneDidEnterBackground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    self.enterBackgroundTime = [NSDate date].timeIntervalSince1970;
}

- (void)loadSplashAdAndShow {
    // 略...
}
@end

如果要位开屏广告添加自定义底部视图,使用initWithSlotId: adSize:构造方法,并设置customBottomView属性,传入的adSizecustomBottomView.frame.size相加应当等于屏幕大小,否则可能会显示异常,example:

CGSize screenSize = UIScreen.mainScreen.bounds.size;
CGSize adSize = CGSizeMake(screenSize.width, screenSize.height * 0.8);;
_splashAd = [[SASplashAd alloc] initWithSlotId:@"ID" adSize:adSize];
_splashAd.customBottomView = [[SADSplashAdCustomBottomView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height * 0.2)];
_splashAd.delegate = self;
[_splashAd loadAd];
上次编辑于: