使用 XHLaunchAd 快速实现app启动广告

XHLaunchAd 的 github 代码地址

1. Cocopods导入

pod 'XHLaunchAd'

2. 在 AppDelegate.m 中加入头文件

#import "XHLaunchAd.h"
#import "RTGlobalWebViewViewController.h" // 自己封装的简单浏览器
#import "JKMainNavigationController.h" // 自己写的NavigationController的父类

3. 在 didFinishLaunchingWithOptions 中添加

/** -------------- 广告业务处理 -------------- */
    [self launchAd];

4. 在AppDelegate.m中添加

#pragma mark - 启动广告

- (void)launchAd {

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    [[JKNetworkingTool sharedNetworkingTool] postDataWithUrl:@"Reward/getStartUpImg" parameters:dic finishedBlock:^(id responseObj, NSError *error) {
        if (error) {
            return ;
        }

        NSDictionary *dic = [responseObj objectForKey:@"data"];

        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = [dic[@"duration"] integerValue];
        //广告frame
//        imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = dic[@"img"];
        //网络图片缓存机制(只对网络图片有效)
        imageAdconfiguration.imageOption = XHLaunchAdImageRefreshCached;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;

        if ([dic[@"type"] isEqualToString:@"1"]) {
            //广告点击打开链接
            imageAdconfiguration.openURLString = [NSString stringWithFormat:@"%@Home/Index/activeInfo/b_id/%@",BASE_H5URL,dic[@"b_id"]];
        }

        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
        //广告显示完成动画时间
        imageAdconfiguration.showFinishAnimate = 0.8;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeTimeText;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;

        //设置要添加的子视图(可选)
        //imageAdconfiguration.subViews = ...

        //显示图片开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];

    }];


}

/**
 *  广告点击事件 回调
 */
- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)openURLString;
{
    if(openURLString)
    {
        //跳转到广告详情页面
        RTGlobalWebViewViewController *vc = [[RTGlobalWebViewViewController alloc] init];
        vc.urlString = openURLString;
        vc.navTitle = @"广告";
        vc.isFromLaunchAd = YES;
        JKMainNavigationController *nav = [[JKMainNavigationController alloc] initWithRootViewController:vc];
        [self.window.rootViewController presentViewController:nav animated:YES completion:nil];

    }
}

你可能感兴趣的:(iOS-OC)