网页缓存:https://www.jianshu.com/p/f3019d511f36、https://blog.csdn.net/leikezhu1981/article/details/68491249
网路请求缓存:https://www.cnblogs.com/wendingding/p/3950198.html
//
// ViewController.m
// WebView
//
// Created by lambo on 2017/1/17.
// Copyright © 2017年 cn.lr. All rights reserved.
//
/*webview 自动计算内容高度----------
//第一种方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
_webTablewView.contentSize = CGSizeMake(320, newFrame.size.height + 64 + KWIDTH - 100);
}
//2.执行js语句 直接获取html文档的dom高度
- (void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloatwebViewHeight =[[webViewstringByEvaluatingJavaScriptFromString:@document.body.offsetHeight]floatValue];
// CGFloat webViewHeight= [[webViewstringByEvaluatingJavaScriptFromString:@document.body.scrollHeight]floatValue];
CGRectnewFrame = webView.frame;
newFrame.size.height= webViewHeight;
webView.frame= newFrame;
}
//方法3.先将UIWebView的高度设为最小,然后再使用sizeThatFits就会返回刚好合适的大小
-(void)webViewDidFinishLoad:(UIWebView*)webVie{
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
CGRect newFrame = webView.frame;
newFrame.size.height = actualSize.height;
webView.frame = newFrame;
}
//方法4.遍历webview子视图 获取UIWebDocumentView高度即实际高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight = 0.0f;
if([webView.subviews count] > 0)
{
UIView *scrollerView = webView.subviews[0];
if([scrollerView.subviews count] >
0)
{
UIView *webDocView = scrollerView.subviews.lastObject;
if ([webDocView isKindOfClass:[NSClassFromString(@UIWebDocumentView)class]])
{
webViewHeight = webDocView.frame.size.height;//获取文档的高度
webView.frame=webDocView.frame;
//更新UIWebView 的高度
}
}
}
}
*/
#import "ViewController.h"
#import
@interface ViewController ()<UIWebViewDelegate,WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
@end
@implementation ViewController
/**webView中的属性==========================
UIWebview 会自动保存上一次的cookie,WKWebview不会。
代理属性重点需要知道代理方法的使用
@property (nullable, nonatomic, assign) id
这个是webView内部的scrollView只读,但是利用这个属性,设置scrollView的代理,就可以控制整个webView的滚动事件
@property(nonatomic, readonly, strong) UIScrollView *scrollView;
webView的请求,这个属性一般在整个加载完成后才能拿到
@property (nullable, nonatomic, readonly, strong) NSURLRequest *request;
A Boolean value indicating whether the receiver can move backward. (read-only)
If YES, able to move backward; otherwise, NO.
如果这个属性为YES,才能后退
@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
A Boolean value indicating whether the receiver can move forward. (read-only)
If YES, able to move forward; otherwise, NO.
如果这个属性为YES,才能前进
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
A Boolean value indicating whether the receiver is done loading content. (read-only)
If YES, the receiver is still loading content; otherwise, NO.
这个属性很好用,如果为YES证明webView还在加载数据,所有数据加载完毕后,webView就会为No
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.
If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.
YES代表网页可以缩放,NO代表不可以缩放
@property (nonatomic) BOOL scalesPageToFit;
设置某些数据变为链接形式,这个枚举可以设置如电话号,地址,邮箱等转化为链接
@property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);
iPhone Safari defaults to NO. iPad Safari defaults to YES
设置是否使用内联播放器播放视频
@property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0);
iPhone and iPad Safari both default to YES
设置视频是否自动播放
@property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0);
iPhone and iPad Safari both default to YES
设置音频播放是否支持ari play功能
@property (nonatomic) BOOL mediaPlaybackAllowsAirPlay NS_AVAILABLE_IOS(5_0);
iPhone and iPad Safari both default to NO
设置是否将数据加载入内存后渲染界面
@property (nonatomic) BOOL suppressesIncrementalRendering NS_AVAILABLE_IOS(6_0);
default is YES
设置用户是否能打开keyboard交互
@property (nonatomic) BOOL keyboardDisplayRequiresUserAction NS_AVAILABLE_IOS(6_0);
IOS7 以后的新特性
这个属性用来设置一种模式,当网页的大小超出view时,将网页以翻页的效果展示,枚举如下:
@property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);
typedef NS_ENUM(NSInteger, UIWebPaginationMode) {
UIWebPaginationModeUnpaginated, //不使用翻页效果
UIWebPaginationModeLeftToRight, //将网页超出部分分页,从左向右进行翻页
UIWebPaginationModeTopToBottom, //将网页超出部分分页,从上向下进行翻页
UIWebPaginationModeBottomToTop, //将网页超出部分分页,从下向上进行翻页
UIWebPaginationModeRightToLeft //将网页超出部分分页,从右向左进行翻页
};
This property determines whether certain CSS properties regarding column- and page-breaking are honored or ignored.
这个属性决定CSS的属性分页是可用还是忽略。默认是UIWebPaginationBreakingModePage
@property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);
设置每一页的长度
@property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);
设置每一页的间距
@property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);
获取页数
@property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);
==============================*/
/**webView的代理方法==========================
// 加载Data数据创建一个webView
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL
// 加载本地HTML字符串(字符串中的内容是html网页代码)用webView调用这个方法
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
// 加载一个请求创建一个webView,吧请求加在到webview网页中
- (void)loadRequest:(NSURLRequest *)request
// 刷新网页
- (void)reload;
// 停止网页加载内容
- (void)stopLoading;
// 后退
- (void)goBack;
// 前进
- (void)goForward;
// 执行JS方法,这个方法可以返回整个网页html代码,
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
======================================*/
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *web=[[UIWebViewalloc]initWithFrame:self.view.frame];
[self.viewaddSubview:web];
[web loadHTMLString:@"
sssssssssss
"baseURL:nil];//加载html
// //==========/webView的使用===============
// UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// self.view = webView;
// webView.delegate=self;
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"Untitled.html" withExtension:nil];//从本地获取的URL
//
NSURL *url = [NSURL URLWithString:@"https:/baidu.com"];//从网络获取的URL
// NSURLRequest *request = [NSURLRequest requestWithURL:url];//默认超时60秒
NSURLRequest *request=[NSURLRequestrequestWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:3];//带超时时间的请求
// [webView loadRequest:request];
//===================================
//=====WKWebview===直接加载js,不是通过URL请求加载//方式4============
// 图片缩放的js代码myFunction()
NSString *js =@"return document.getElementsByTagName('p')[0].innerHTML";
// NSString *js = @"myFunction();";
// 根据JS字符串初始化WKUserScript对象
WKUserScript *script = [[WKUserScript alloc]initWithSource:js injectionTime:WKUserScript InjectionTimeAtDocumentStartforMainFrameOnly:YES];
// WKUserContentController *userContentController = [[WKUserContentController alloc] init];
创建网页配置对象, 根据生成的WKUserScript对象,初始化WKWebViewConfiguration
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
[config.userContentController addUserScript:script];
// 创建设置对象
WKPreferences *preference = [[WKPreferencesalloc]init];
// 设置网页字体大小
preference.minimumFontSize =30;
// 设置偏好设置对象
config.preferences = preference;
//创建WKWebView
WKWebView* webView = [[WKWebViewalloc]initWithFrame:self.view.bounds configuration:config];
webView.navigationDelegate=self;
self.view=webView;
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"Untitled.html" withExtension:nil];//从本地获取的URL
//====== 这个也可以获取本地路径 //网页文件全路径
// NSString*filePath = [[NSBundle mainBundle] pathForResource:@"Untitled.html" ofType:nil];
// // 创建URL对象:指定要加载资源的路径
// ======= NSURL *URL = [NSURL fileURLWithPath:filePath];
//直接加在html==============
[webView loadHTMLString:@"
这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊这是爱鱼app,iOS版本我的老家就住在这个屯我是这个屯里土生图章的人啊