展示网页界面
1.Safari openURL :自带很多功能(进度条,刷新,前进,倒退等等功能,必须要跳出当前应用)
2.UIWebView(没有功能),在当前应用打开网页,并且有safari,自己实现,UIWebView不能实现进度条
3.SFSafariViewController:专门用来展示网页 需求:既想要在当前应用展示网页,有想要safari功能,导入<SafariServices/SafariServices.h>
NSURL *url = [NSURL URLWithString:url];
SFSafariViewController *safarVC = [[SFSafariViewController alloc] initWithURL:url];
[self presentViewController:safarVC animated:YES completion:nil];
4.WKWebView:iOS8(UIWebView升级版本,添加功能 1.监听进度 2.缓存),导入<WebKit/WebKit.h>
//创建WKWebView
WKWebView *webView = [[WKWebView alloc] init];
[self.contentView addSubview:webView];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
/*
观察者KVO监听属性改变
Observer:观察者
KeyPath:观察webView的什么属性
options:NSKeyValueObservingOptionNew观察新值得改变
KVO注意一定要移除
*/
//后退
[self.webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
//前进
[self.webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
//标题
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
//进度条
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
只要观察到有新值改变就会调用这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
self.backItem.enabled = self.webView.canGoBack;
self.forwardItem.enabled = self.webView.canGoForward;
self.title = self.webView.title;
self.progressView.progress = self.webView.estimatedProgress;
self.progressView.hidden = self.webView.estimatedProgress >= 1;
}
移除KVO
- (void)dealloc{
[self.webView removeObserver:self forKeyPath:@"canGoBack"];
[self.webView removeObserver:self forKeyPath:@"canGoForward"];
[self.webView removeObserver:self forKeyPath:@"title"];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
计算缓存文件大小
计算整个应用的缓存数据 => 沙盒(cache) => 缓存文件夹尺寸
1、SDWebImage:做了缓存
NSInteger cacheSize = [SDImageCache sharedImageCache].getSize;
2、手动计算
+ (void)getFileSize:(NSString *)directoryPath completion:(void (^)(NSInteger))completion{
/*
attributesOfItemAtPath:指定文件路径获取属性
把所有的文件大小都加起来
*/
__block NSInteger fileSize = 0;
//获取文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
//判断是否是文件夹
BOOL isDirctory;
BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirctory];
if (!isExist || !isDirctory) {
/**
* 抛出异常
* name :异常名称
* reason :报错原因
**/
NSException *exception = [NSException exceptionWithName:@"directoryPathError" reason:@"需要传入的是一个存在的文件路径" userInfo:nil];
[exception raise];
};
//开启异步运算
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//获取文件下的所有子路径
NSArray *fileArray = [manager subpathsAtPath:directoryPath];
//遍历所有文件夹 一个一个加起来
for (NSString *subPath in fileArray) {
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
//�判断是否是隐藏文件
if ([filePath containsString:@".DS"]) continue;
//判断是否是文件夹
BOOL isDirctory;
BOOL isExist = [manager fileExistsAtPath:filePath isDirectory:&isDirctory];
if (!isExist || isDirctory) continue;
//获取文件属性
NSDictionary *dict = [manager attributesOfItemAtPath:filePath error:nil];
fileSize += [dict fileSize];
}
//计算完成block回调
dispatch_sync(dispatch_get_main_queue(), ^{
if (completion) {
completion(fileSize);
}
});
});
}
3.清除缓存
+ (void)clearFileCache:(NSString *)directoryPath{
//获取文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
//判断是否是文件夹
BOOL isDirctory;
BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirctory];
if (!isExist || !isDirctory) {
/**
* 抛出异常
* name :异常名称
* reason :报错原因
**/
NSException *exception = [NSException exceptionWithName:@"directoryPathError" reason:@"需要传入的是一个存在的文件路径" userInfo:nil];
[exception raise];
};
//获取cache下的所有文件,不包括子路径的子路径
NSArray *subArray = [manager contentsOfDirectoryAtPath:directoryPath error:nil];
//删除所有文件
for (NSString *subPath in subArray) {
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
//删除路径
[manager removeItemAtPath:filePath error:nil];
}
}
网友评论