當應用發展到一定階段,一般都會在feeds流中插入廣告,來進行廣告的變現,這是每個應用都要進行的過程。 比如微信朋友圈,微博,QQ空間等,不列舉了,一般有feeds流的都會有廣告。
當你的應用也需要在原有的業務上插入廣告,你會怎麼做? 可能你會直接叫接口把廣告跟業務數據合並下,就下發給你。然後你在業務層去各種判斷。
曾經這樣做的程序猿應該很多,累嗎? 這樣子的插入,需要去改各種代碼,還可能在一個微小的角落 可能直接調用了
- (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行代碼。哦,還有一個聲明。
///只是聲明,防止提前釋放
@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`文件,核心代碼就是設置數據回調,跟選擇插入的位置。
- (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回調的一致性,方便把舊的廣告代碼遷移過來。
@protocol IMYAOPTableViewDelegate ; @protocol IMYAOPTableViewDataSource
接下來就是要實現TableView的廣告回調了, 其實下面兩個回調是不會調用的,就是返回數據源數量的回調,因為這個是由業務模塊決定的。但是沒實現xcode會有警告,所以也可以順手寫上。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-(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:
如果有興趣可以看具體的源碼:源碼傳送門