《iOS-Feeds廣告插入方案》-- 不入侵任何的業(yè)務(wù)代碼

最近在CocoaChina看到很多小伙伴都分享了應(yīng)用內(nèi)插入廣告的經(jīng)驗,但是業(yè)務(wù)層需要太多的改動。我認為這套框架是很適合半路接廣告的小伙伴的,所以開源出來,希望能給大家做點貢獻。

當(dāng)應(yīng)用發(fā)展到一定階段,一般都會在feeds流中插入廣告,來進行廣告的變現(xiàn),這是每個應(yīng)用都要進行的過程。 比如微信朋友圈,微博,QQ空間。。。 不列舉了,一般有feeds流的都會有廣告。

當(dāng)你的應(yīng)用也需要在原有的業(yè)務(wù)上插入廣告,你會怎么做? 可能你會直接叫接口把廣告跟業(yè)務(wù)數(shù)據(jù)合并下,就下發(fā)給你。然后你在業(yè)務(wù)層去各種判斷。

曾經(jīng)這樣做的程序猿應(yīng)該很多,累嗎? 這樣子的插入,需要去改各種代碼,還可能在一個微小的角落 可能直接調(diào)用了
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
然后返回的類型不對,應(yīng)用直接Crash了

出欄

現(xiàn)在這個框架就是出來解決這種情況的?。≡摽蚣芮盁o古人開源(可能我沒搜到),個人覺得覺得沒有比這套更好的解決方案了。

要解決的目標(biāo):

  1. 舊代碼少改動,或者不改動。
  2. 業(yè)務(wù)跟廣告模塊分離
  3. 廣告模塊可以獲取真實數(shù)據(jù)源。
  4. 上手簡單

用法:

我先下載了 YYKit,YYKit作者對代碼的極致追求也是我喜歡的。主要原因是因為它里面有Feeds(Twitter,微博)的demo。就像我們以前的業(yè)務(wù)代碼,夠復(fù)雜,邏輯夠多。

開始

我對demo的具體代碼是不了解的,但是有了IMYAOPTableView,我已經(jīng)可以不需要懂內(nèi)部的實現(xiàn),就可以對它進行廣告的插入。

先找到了初始化 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文件,核心代碼就是設(shè)置數(shù)據(jù)回調(diào),跟選擇插入的位置。


- (void)injectTableView
{
    [self.aopUtils.tableView registerClass:[UITableViewCell class]  forCellReuseIdentifier:@"AD"];

    ///廣告回調(diào),跟TableView的Delegate,DataSource 一樣。
    self.aopUtils.delegate = self;
    self.aopUtils.dataSource = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self insertRows];
    });
}
///簡單的rows插入
- (void)insertRows
{
    NSMutableArray<IMYAOPTableViewInsertBody*>* insertBodys = [NSMutableArray array];
    ///隨機生成了5個要插入的位置
    for (int i = 0 ; i< 5; i++) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:arc4random()%10 inSection:0];
        [insertBodys addObject:[IMYAOPTableViewInsertBody insertBodyWithIndexPath:indexPath]];
    }
    ///清空 舊數(shù)據(jù)
    [self.aopUtils insertWithSections:nil];
    [self.aopUtils insertWithIndexPaths:nil];
    
    ///插入 新數(shù)據(jù), 同一個 row 會按數(shù)組的順序 row 進行 遞增
    [self.aopUtils insertWithIndexPaths:insertBodys];

    ///調(diào)用tableView的reloadData,進行頁面刷新
    [self.aopUtils.tableView reloadData];
}

廣告的回調(diào),其實看代碼,他們也是繼承了TableView Delegate,跟DataSource,保持跟TableView回調(diào)的一致性,方便把舊的廣告代碼遷移過來。


    @protocol IMYAOPTableViewDelegate <UITableViewDelegate>;
    @protocol IMYAOPTableViewDataSource <UITableViewDataSource>

接下來就是要實現(xiàn)TableView的廣告回調(diào)了, 其實下面兩個回調(diào)是不會調(diào)用的,就是返回數(shù)據(jù)源數(shù)量的回調(diào),因為這個是由業(yè)務(wù)模塊決定的。但是沒實現(xiàn)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 , 如不播放,可點擊到新頁面試試):

如果有興趣可以看具體的源碼:傳送門

美柚公司內(nèi)推,有需要可以私信我:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容