开屏广告
此处通过第三方库实现,GitHub地址:XHLaunchAd,
具体使用方法, 自定义类,实现XHLaunchAdDelegate, 在类被夹在时候会设置开屏广告的相关信息.
@interface TGLaunchAdManager ()<XHLaunchAdDelegate>
@end
@implementation TGLaunchAdManager
+ (void)load {
[self shareManager];
}
+ (TGLaunchAdManager *)shareManager {
static TGLaunchAdManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[TGLaunchAdManager alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
// 初始化启动页广告
[self _setupLaunchAd];
}];
}
return self;
}
- (void)_setupLaunchAd {
//设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
[XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
//1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
//2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
//3.数据获取成功,配置广告数据后,自动结束等待,显示广告
//注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
[XHLaunchAd setWaitDataDuration:3];
// 获取通用信息
TGAppInfoModel *model = [TGSystemHelper shareInstance].appInfoModel;
if (!KObjectIsEmpty(model)) {
//配置广告数据
XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
//广告停留时间
imageAdconfiguration.duration = 3;
//广告frame
imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
//广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
imageAdconfiguration.imageNameOrURLString = model.app_page.welcome_page;
//设置GIF动图是否只循环播放一次(仅对动图设置有效)
imageAdconfiguration.GIFImageCycleOnce = NO;
//缓存机制(仅对网络图片有效)
//为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
imageAdconfiguration.imageOption = XHLaunchAdImageCacheInBackground;
//图片填充模式
imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
//广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
imageAdconfiguration.openModel = @"https://github.com/CoderZhuXH/XHLaunchAd";
//广告显示完成动画
imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
//广告显示完成动画时间
imageAdconfiguration.showFinishAnimateTime = 0.5;
//跳过按钮类型
imageAdconfiguration.skipButtonType = SkipTypeTimeText;
//后台返回时,是否显示广告
imageAdconfiguration.showEnterForeground = NO;
//图片已缓存 - 显示一个 "已预载" 视图 (可选)
if([XHLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:model.app_page.welcome_page]]){
//设置要添加的自定义视图(可选)
imageAdconfiguration.subViews = [self launchAdSubViews_alreadyView];
}
//显示开屏广告
[XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}
}
/// 开屏广告点击事件
- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint {
if(openModel==nil) return;
/*
BaseWebController *vc = [[BaseWebController alloc] initWithViewModel:nil];
vc.loadingProgressColor = [UIColor grayColor];
vc.canDownRefresh = YES;
NSString *urlString = (NSString *)openModel;
vc.url = urlString;
[[AppDelegate sharedDelegate].navigationControllerStack.topNavigationController pushViewController:vc animated:YES];
*/
}
- (NSArray<UIView *> *)launchAdSubViews_alreadyView {
CGFloat y = KISIPhoneXSeries() ? 46 : 22;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-140, y, 60, 30)];
label.text = @"已预载";
label.font = [UIFont systemFontOfSize:12.f];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.layer.cornerRadius = 15.0;
label.layer.masksToBounds = YES;
label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
return [NSArray arrayWithObject:label];
}
@end