SFSafariViewController在网页中的应用

//
//  MeViewController.m
//  01-BuDeJie
//
//  Created by 苗冬 on 2017/3/15.
//  Copyright © 2017年 苗冬. All rights reserved.
//

#import "MeViewController.h"
#import "SettingViewController.h"
#import "MeSquareCollectionViewCell.h"
#import "MeSquareModel.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD.h>
#import <SafariServices/SafariServices.h> //iOS9新出的safair框架



static NSString *const reuseIdentifier = @"reuseIdentifier" ;
//总列数:
static NSInteger columns = 4 ;
//间距线:
static CGFloat margin = 1 ;
//cell的高宽
#define itemWH (screenW - (columns - 1) * margin) / columns

@interface MeViewController () <UICollectionViewDataSource , UICollectionViewDelegate/* , SFSafariViewControllerDelegate*/>

@property (nonatomic , strong) NSMutableArray *dataSourceArray ;

@property (nonatomic , weak) UICollectionView *collectionView ;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航条:
    [self setupNavigationBar] ;
    //设置tableView的底部视图:
    [self setupFooterView] ;
    //网络请求:
    [self loadData] ;
    //处理cell的间距(默认tableView就有额外的头部和尾部的间距):
    self.tableView.sectionHeaderHeight = 0 ;
    self.tableView.sectionFooterHeight = 10 ;
    //内容视图向上偏移25个单位,保证每一个cell都有10个间距的间距:
    self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0) ;
    
}


#pragma mark - 设置导航条
- (void)setupNavigationBar {
    //夜间模式按钮:
    UIBarButtonItem *nightItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] andSelectedImage:[UIImage imageNamed:@"mine-sun-icon-click"] addTarget:self action:@selector(nightItemAction:)] ;
    //设置按钮:
    UIBarButtonItem *settingItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"] andHighLightedImage:[UIImage imageNamed:@"mine-setting-icon-click"] addTarget:self action:@selector(settingItemAction)] ;
    self.navigationItem.rightBarButtonItems = @[settingItem , nightItem] ;
    //titleView:
    self.navigationItem.title = @"我的" ;
}

#pragma mark - 点击夜间模式按钮
- (void)nightItemAction:(UIButton *)selectedButton {
    selectedButton.selected = !selectedButton.selected ;
}


#pragma mark - 设置底部视图
- (void)setupFooterView {
    /*
     初始化一个CollectionView的注意事项:
        1.设置一个流水布局 ;
        2.cell必须要注册 ;
        3.cell必须要自定义 ;
     */
    // 1.创建布局:
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init] ;
    flowLayout.minimumLineSpacing = margin ;
    flowLayout.minimumInteritemSpacing = margin ;
    //设置cell的尺寸:
    flowLayout.itemSize = CGSizeMake(itemWH, itemWH) ;
    // 2.创建collectionView:
    //在tableView的底部视图,顶部视图内,宽度,位置点是不用管理的!
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:flowLayout] ;
    // 3.将 collectionView 放到tableView 的footer上:
    self.tableView.tableFooterView = collectionView ;
    collectionView.backgroundColor = self.tableView.backgroundColor ;
    //让collectionView展示一个一个的小格子,得遵守数据源协议:
    collectionView.dataSource = self ;
    collectionView.delegate = self ;
    //collectionView禁止滚动:
    collectionView.scrollEnabled = NO ;
    //注册cell:
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MeSquareCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier] ;
    _collectionView = collectionView ;
    
}

#pragma mark - 网络请求
- (void)loadData {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary] ;
    //拼接请求参数:
    parameters[@"字段"] = @"内容" ;
    parameters[@"字段"] = @"内容" ;
    parameters[@"字段"] = @"内容" ;
    parameters[@"字段"] = @"内容" ;
    //创建会话管理者并发送请求url:
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
    [manager GET:@"接口数据" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        [SVProgressHUD dismiss] ;
        //要的是square_list这个字段 , 把这个字段字典转模型:
        NSArray *dictArray = responseObject[@"square_list"] ;
        //接下来创建模型数组,把字典数组dictArray添加到模型数组里:
        _dataSourceArray = [MeSquareModel mj_objectArrayWithKeyValuesArray:dictArray] ;

        //处理数据:判断是否多余或者少数量为4的倍数:
        [self resolveDataSource] ;
        
        //有了数据之后要重新设置collectionView的高度(根据内容自适应高度):
        //collectionView.height = row * itemH ;
        //Rows = (conut - 1) / columns + 1 ;
        NSInteger count = _dataSourceArray.count ;
        NSInteger rows = (count - 1) / columns + 1 ;
        //collectionView高度动态化:
        self.collectionView.height = rows * itemWH ;
        //tableView的滚动范围:
        //tableView的滚动范围不用人为管理 , 是系统自动会根据内容去计算:
        //重新设置collectionView为tableView的footerView:
        //我的collectionView的高度一开始设置了一个300的高度,然后tableView就一直认为300的高度是tableView的底部视图(即:collectionView)的高度 , tableView并不知道collectionView的高度根据内容自适应更新了!所以我在这里重新设置collectionView为tableView的底部视图让tableView也更新一下,根据最新的底部视图高度更新一下!
        self.tableView.tableFooterView = self.collectionView ;
        
        //有了数据之后,刷新表格:
        [self.collectionView reloadData] ;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"网络请求失败/(ㄒoㄒ)/~~"] ;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss] ;
        });
    }] ;
}


#pragma mark - 处理请求完成的数据
- (void)resolveDataSource {
    //判断缺几个?!
    //3 % 4 = 3 ---> columns - 3 = 1 ;
    NSInteger count = _dataSourceArray.count ;
    NSInteger extra = count % columns ;
    
    if (extra) {
        extra = columns - extra ;
        for (int i = 0; i < extra; i++) {
            MeSquareModel *squareModel = [[MeSquareModel alloc] init] ;
            [_dataSourceArray addObject:squareModel] ;
        }
    }
}

#pragma mark - 点击设置按钮
- (void)settingItemAction {
    //跳转到设置界面:
    SettingViewController *settingVC = [[SettingViewController alloc] init] ;
    //这个方法必须放在push之前才会管用:
    settingVC.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:settingVC animated:YES] ;
}


#pragma mark - <UICollectionViewDataSource>
//刷新表格[self.collectionView reloadData]之后就会调用数据源协议代理方法方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArray.count ; //一共35个item ;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MeSquareCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath] ;
    cell.backgroundColor = [UIColor whiteColor] ;
    //把这些模型的数据展示到对应的cell上:
    cell.meSquareModel = self.dataSourceArray[indexPath.item] ;
    return cell ;
}

#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"被调用方法为: %s , 所在行数为:第 %zd 行" , __FUNCTION__ , __LINE__) ;
    
    //跳转界面:push到webView:
    /*
     1.safari: openURL方法 ;
     2.UIWebView: 展示网页 ;
     3.safair框架--#import <SafariServices/SafariServices.h> //iOS9新出的safair框架
     SFSafariViewController:专门用来展示网页的控制器 ;
     */
    MeSquareModel *squareModel = self.dataSourceArray[indexPath.item] ;
    NSURL *url = [NSURL URLWithString:squareModel.url] ;
    //有些事跳转到网页的 , 有些是跳转到其他界面的 , 只需要判断url字符串里有没有http字段:
    if (![squareModel.url containsString:@"http"]) return ;
    SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url] ;
//    safariVC.delegate = self ;
    [self presentViewController:safariVC animated:YES completion:nil] ;
    
}


//#pragma mark - <SFSafariViewControllerDelegate>
//推荐我们用modal方式(就不用遵守这个代理协议了!)
//- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
////    [self.navigationController popViewControllerAnimated:YES] ;
//    [self dismissViewControllerAnimated:YES completion:nil] ;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end


愿编程让这个世界更美好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,264评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,549评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,389评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,616评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,461评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,351评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,776评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,414评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,722评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,760评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,537评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,381评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,787评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,030评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,304评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,734评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,943评论 2 336

推荐阅读更多精彩内容