如何优雅的插入广告——代码篇

来源:li6185 投稿

当应用发展到一定阶段,一般都会在feeds流中插入广告,来进行广告的变现,这是每个应用都要进行的过程。 比如微信朋友圈,微博,QQ空间等,不列举了,一般有feeds流的都会有广告。

当你的应用也需要在原有的业务上插入广告,你会怎么做? 可能你会直接叫接口把广告跟业务数据合并下,就下发给你。然后你在业务层去各种判断。 

曾经这样做的程序猿应该很多,累吗? 这样子的插入,需要去改各种代码,还可能在一个微小的角落 可能直接调用了  

1
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

然后返回的类型不对,应用直接Crash了

出栏

现在有个新框架来解决这种情况啦,该框架前无古人开源(可能我没搜到),超级一流xxxx(呃,不懂吹啥了,发现不说大一点,都没人敢用,有bug可以提哦。目前公司应用加起来,总用户突破1个亿,日活700W。其实代码的稳定性还是可以放心的。)

要解决的目标:

1. 旧代码少改动,或者不改动。

2. 业务跟广告模块分离

3. 广告模块可以获取真实数据源。

4. 上手简单

用法

我先下载了YYKit,YYKit作者对代码的极致追求也是我喜欢的。主要原因是因为它里面有Feeds(Twitter,微博)的demo。就像我们以前的业务代码,够复杂,逻辑够多。

开始

我对demo的具体代码是不了解的,但是有了IMYAOPTableView,我已经可以不需要懂内部的实现,就可以对它进行广告的插入。 

先找到了初始化 Twitter,微博 的ViewController地方,并且获取TableView的AopUtils。只有3行代码。哦,还有一个声明。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
///只是声明,防止提前释放
@property (nonatomic, strong) IMYAOPDemo* aopDemo;
///插入3行代码的地方
- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NSString *className = self.classNames[indexPath.row];
     Class  class  = NSClassFromString(className);
     if  ( class ) {
         UIViewController *ctrl =  class . new ;
         
         ///begin 插入3行代码
         self.aopDemo = [IMYAOPDemo  new ];
         UITableView* feedsTableView = [ctrl valueForKey:@ "tableView" ];
         self.aopDemo.aopUtils = feedsTableView.aop_utils;
         ///end
         
         ctrl.title = _titles[indexPath.row];
         self.title = @ " " ;
         [self.navigationController pushViewController:ctrl animated:YES];
     }
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这个时候需要新建一个,维护广告逻辑的类,简单的建立了个`IMYAOPDemo`文件,核心代码就是设置数据回调,跟选择插入的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- ( void )injectTableView
{
     [self.aopUtils.tableView registerClass:[UITableViewCell  class ]  forCellReuseIdentifier:@ "AD" ];
 
     ///广告回调,跟TableView的Delegate,DataSource 一样。
     self.aopUtils.delegate = self;
     self.aopUtils.dataSource = self;
     
     dispatch_async(dispatch_get_main_queue(), ^{
         [self insertRows];
     });
}
///简单的rows插入
- ( void )insertRows
{
     NSMutableArray* insertBodys = [NSMutableArray array];
     ///随机生成了5个要插入的位置
     for  ( int  i = 0 ; i< 5; i++) {
         NSIndexPath* indexPath = [NSIndexPath indexPathForRow:arc4random() inSection:0];
         [insertBodys addObject:[IMYAOPTableViewInsertBody insertBodyWithIndexPath:indexPath]];
     }
     ///清空 旧数据
     [self.aopUtils insertWithSections:nil];
     [self.aopUtils insertWithIndexPaths:nil];
     
     ///插入 新数据, 同一个 row 会按数组的顺序 row 进行 递增
     [self.aopUtils insertWithIndexPaths:insertBodys];
 
     ///调用tableView的reloadData,进行页面刷新
     [self.aopUtils.tableView reloadData];
}

广告的回调,其实看代码,他们也是继承了TableView Delegate,跟DataSource,保持跟TableView回调的一致性,方便把旧的广告代码迁移过来。

1
2
@protocol IMYAOPTableViewDelegate ;
@protocol IMYAOPTableViewDataSource

接下来就是要实现TableView的广告回调了, 其实下面两个回调是不会调用的,就是返回数据源数量的回调,因为这个是由业务模块决定的。但是没实现xcode会有警告,所以也可以顺手写上。

1
2
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@ "AD" ];
     if (cell.contentView.subviews.count == 0) {
         CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
         CGFloat imageHeight = 162 * (screenWidth/320.0f);
         UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, imageHeight)];
         imageView.image = [UIImage imageNamed:@ "aop_ad_image.jpeg" ];
         imageView.layer.borderColor = [UIColor blackColor].CGColor;
         imageView.layer.borderWidth = 1;
         [cell.contentView addSubview:imageView];
         
         UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(200, 100, 200, 50)];
         label.text = @ "不要脸的广告!" ;
         [cell.contentView addSubview:label];
     }
     return  cell;
}
-( void )tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@ "插入的cell要显示啦" );
}
- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@ "被点击了> <"  message:[NSString stringWithFormat:@ "我的位置: %@" ,indexPath] delegate:nil cancelButtonTitle:@ "哦~滚"  otherButtonTitles:nil];
     [alertView show];
}

效果图是个gif 如果不会动可以看 git 的readme:

如果有兴趣可以看具体的源码:源码传送门

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值