说明:

本SDK(MobNewsSDK)既可独立使用,也可以组合广告SDK(MobADSDK)使用,独立使用则没有广告,组合广告SDK可在feed流和资讯详情展示广告,实现App收益。

一、环境依赖

1.1.1 info.plist配置

  • App Transport Security Settings -> Allow Arbitrary Loads:YES

1.1.2 Pod依赖

source 'https://gitee.com/mobad/Specs.git' # 自建仓库,须放在 CocoaPods 前面
source 'https://github.com/CocoaPods/Specs.git'

# platform :ios, '9.0'
target 'podTest' do
  # use_frameworks!
  
  pod 'AFNetworking'
  pod 'SDWebImage'
  pod 'MJExtension'
  pod 'ReactiveObjC'
  pod 'SVPullToRefresh'
  pod 'MobNewsSDK', '~> 2.3.2'
  # Pods for podTest
end

ReactiveObjC与ReactiveObjC-bm的区别、
安装CocoaPods及广告所需的Pod依赖请见MobADSDK说明文档

二、初始化

2.1.1 最简初始化

    // 创建初始化配置实例
    MobNewsConfigModel *config = [[MobNewsConfigModel alloc] init];
    // 渠道id,必填(由我司分配),下面的是测试用
    config.appId = @"ba0063bfbc1a5ad878";

    // 初始化SDK
    BOOL result = [MobNewsSDKApi setupWithConfig:config];
    // 设置回调代理
    [MobNewsSDKApi setCallbackDelegate:self];

2.2 自定义配置初始化

    // 创建初始化配置实例
    MobNewsConfigModel *config = [[MobNewsConfigModel alloc] init];
    // 渠道id,必填(由我司分配),下面的是测试用
    config.appId = @"ba0063bfbc1a5ad878";
    // 主色调
    config.mainTintColor = [UIColor colorWithRed:243/255.0 green:186/255.0 blue:74/255.0 alpha:1];
    // feed流下拉加载的背景色
    config.pullRefreshTipBackColor = [UIColor redColor];
    // feed流下拉加载的提示语
    config.pullRefreshTipText = @"为您推荐了%zd条更新";
    // feed流中如果标题包含过滤词,则不显示
    config.feedFilter = [NSSet setWithObjects:@"恐怖", @"杀人", nil];
    // SDK接入方的用户id(如果已登录)
    config.userId = @"userId";
    
#if 0
    // 设置自定义倒计时类(如果需要)
    config.CustomCountDownViewClass = [CustomCountDownView class];
#else
    // 定制内部默认倒计时UI(如果不设置,即使用默认ui)
    config.countDownViewBackgroundColor = [UIColor whiteColor];
    config.countDownViewCircleColor = [UIColor redColor];
    config.countDownViewTextColor = [UIColor redColor];
    config.countDownViewTextFont = [UIFont systemFontOfSize:25 weight:UIFontWeightSemibold];
#endif

    // 初始化SDK
    BOOL result = [MobNewsSDKApi setupWithConfig:config];
    // 设置回调代理
    [MobNewsSDKApi setCallbackDelegate:self];

    NSLog(@"MobNewsSDK setup result:%d", result);
    NSLog(@"MobNewsSDK version:%@", [MobADSDKApi sdkVersion]);

三、资讯

3.1 预览

3.1.1 tabBar嵌入资讯多/单频道页

资讯

3.1.2 下一页打开资讯多频道页

资讯

3.2 接口

3.2.1 调用接口

/**
 * 返回多频道的ViewController
 * @param params 非下级页传nil,下级页可传入@{@"title": @"资讯"}设置导航标题
 * @return 多频道的ViewController实例
 */
+ (UIViewController *)multiTabFeedListWithParams:(NSDictionary *)params;

/**
 * 返回单频道的ViewController
 * @param channel 频道名称,如:推荐,热点,科技。。。(多频道的ViewController中的channel)
 * @return 单频道的ViewController实例
 */
+ (UIViewController *)singleTabFeedListWithChannel:(NSString *)channel;

3.2.2 回调接口

/**
* SDK 资讯详情打开成功
* @param countDownView 倒计时视图实例
* @param info 包含 url、type
*
*/
- (void)news_newsDetailDidLoadSuccessWithInfo:(NSDictionary *)info countDownView:(UIView<LKADCountDownViewProtocol> *)countDownView;

/**
* SDK 资讯详情关闭
* @param info 包含 url、type
*
*/
- (void)news_newsDetailDidCloseWithInfo:(NSDictionary *)info;

/**
* SDK 资讯详情倒计时结束
* @param countDownView 倒计时视图实例
* @param info 包含 url、type
*
*/
- (void)news_newsDetailDidCountDownEndWithInfo:(NSDictionary *)info countDownView:(UIView<LKADCountDownViewProtocol> *)countDownView;

/**
* SDK 资讯详情点击倒计时
* @param countDownView 倒计时视图实例
*
*/
- (void)news_newsDetailCountDownViewDidClick:(UIView<LKADCountDownViewProtocol> *)countDownView;

3.3 示例代码:

3.3.1 tabBar嵌入资讯多/单频道页

在UITabBarController viewDidLoad() 里设置:

#import <MobNewsSDK/MobNewsSDKApi.h>

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1];
    UIViewController *multiTabVC = [MobNewsSDKApi multiTabFeedListWithParams:nil];
    multiTabVC.tabBarItem = item1;
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:multiTabVC];
    
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:2];
    UIViewController *singleTabVC = [MobNewsSDKApi singleTabFeedListWithChannel:@"推荐"];
    singleTabVC.tabBarItem = item2;
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:singleTabVC];
    navi2.navigationBar.translucent = NO;
    singleTabVC.navigationItem.title = @"推荐";
    
    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:3];
    UIViewController *vc = [[ViewController alloc] init];
    vc.tabBarItem = item3;
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:vc];
    
    UITabBarItem *item4 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:4];
    UIViewController *vc2 = [[ViewController2 alloc] init];
    vc2.tabBarItem = item4;
    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:vc2];
    
    self.viewControllers = @[navi1, navi2, navi3, navi4];
}

3.3.2 下一页打开资讯多频道页

// push
- (IBAction)pushMultiTab:(id)sender {
    UIViewController *multiTabVC = [MobNewsSDKApi multiTabFeedListWithParams:@{@"title": @"资讯"}];
    [self.navigationController pushViewController:multiTabVC animated:YES];
}

// present
- (IBAction)presentMultiTab:(id)sender {
    UIViewController *multiTabVC = [MobNewsSDKApi multiTabFeedListWithParams:@{@"title": @"资讯"}];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:multiTabVC];
    navi.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navi animated:YES completion:nil];
}

3.3.3 回调

在这个回调里可以根据不同资讯类型设置不同的计时时长,同时可以设置是否需要用户滑动才能继续计时

// 资讯详情加载成功回调
- (void)news_newsDetailDidLoadSuccessWithInfo:(NSDictionary *)info countDownView:(UIView<LKADCountDownViewProtocol> *)countDownView {
    NSLog(@"news detail load success:%@", info);
    NSString *type = info[@"type"];
    NSInteger duration = 5;
    // 可根据不同资讯类型设置不同时长
    NSInteger scrollDelay = 2;
    if ([type isEqualToString:@"Article"]) {
        duration = 5;
    } else if ([type isEqualToString:@"Video"]) {
        duration = 10;
        scrollDelay = 0;
    } else if ([type isEqualToString:@"Ad"]) {
        duration = 15;
    }
    
    CGSize ss = [UIScreen mainScreen].bounds.size;
    // App 判断是否需要显示倒计时
    BOOL needShow = YES;
    if (needShow) {
        // 开始倒计时并设置倒计时时长和位置
        [countDownView startWithDuration:duration frame:CGRectMake(ss.width - 100, ss.height - 200, 80, 80) scrollDelay:scrollDelay];
    }
}

// 资讯详情倒计时结束回调
- (void)news_newsDetailDidCountDownEndWithInfo:(NSDictionary *)info countDownView:(UIView<LKADCountDownViewProtocol> *)countDownView {
    NSLog(@"news detail count down end:%@", info);
    
    // 模拟网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 根据请求结果显示奖励提示
        [countDownView showRewardWithInfo:[NSString stringWithFormat:@"+%d", arc4random_uniform(10)]];
    });
}

- (void)news_newsDetailCountDownViewDidClick:(UIView<LKADCountDownViewProtocol> *)countDownView {
    NSLog(@"news detail view did click");
}

- (void)news_newsDetailDidCloseWithInfo:(NSDictionary *)info {
    NSLog(@"news detail did close:%@", info);
}