#
import
<WebKit/WebKit.h>
@interface
ViewController
(
)
<
WKNavigationDelegate
,
WKUIDelegate
,
WKScriptMessageHandler
>
@property
(
nonatomic
,
strong
)
WKWebView
*
webView
;
@property
(
nonatomic
,
assign
)
BOOL shouldStartLoadWithRequest
;
@implementation
ViewController
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
]
;
NSURL
*
url
=
[
NSURL URLWithString
:
@"https://www.baidu.com"
]
;
NSURLRequest
*
request
=
[
NSURLRequest requestWithURL
:
url
]
;
[
self
.
webView loadRequest
:
request
]
;
-
(
void
)
userContentController
:
(
WKUserContentController
*
)
userContentController didReceiveScriptMessage
:
(
WKScriptMessage
*
)
message
{
if
(
[
@"NativeMethod"
isEqualToString
:
message
.
name
]
)
{
if
(
[
@"close"
isEqualToString
:
message
.
body
]
)
{
[
self
.
webView
.
configuration
.
userContentController removeScriptMessageHandlerForName
:
@"NativeMethod"
]
;
#
pragma
mark - WKNavigationDelegate代理方法
如果实现了代理方法,一定要在decidePolicyForNavigationAction和decidePolicyForNavigationResponse方法中的回调设置允许跳转。
typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
WKNavigationActionPolicyCancel, // 取消跳转
WKNavigationActionPolicyAllow, // 允许跳转
} API_AVAILABLE(macosx(10.10), ios(8.0));
-
(
void
)
webView
:
(
WKWebView
*
)
webView decidePolicyForNavigationAction
:
(
WKNavigationAction
*
)
navigationAction decisionHandler
:
(
void
(
^
)
(
WKNavigationActionPolicy
)
)
decisionHandler
{
NSLog
(
@"1-------在发送请求之前,决定是否跳转 -->%@"
,
navigationAction
.
request
)
;
if
(
self
.
shouldStartLoadWithRequest
)
{
decisionHandler
(
WKNavigationActionPolicyAllow
)
;
}
else
{
decisionHandler
(
WKNavigationActionPolicyCancel
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didStartProvisionalNavigation
:
(
WKNavigation
*
)
navigation
{
NSLog
(
@"2-------页面开始加载时调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView decidePolicyForNavigationResponse
:
(
WKNavigationResponse
*
)
navigationResponse decisionHandler
:
(
void
(
^
)
(
WKNavigationResponsePolicy
)
)
decisionHandler
{
NSLog
(
@"3-------在收到响应后,决定是否跳转"
)
;
decisionHandler
(
WKNavigationResponsePolicyAllow
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didCommitNavigation
:
(
WKNavigation
*
)
navigation
{
NSLog
(
@"4-------当内容开始返回时调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didFinishNavigation
:
(
WKNavigation
*
)
navigation
{
NSLog
(
@"5-------页面加载完成之后调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didFailProvisionalNavigation
:
(
WKNavigation
*
)
navigation
{
NSLog
(
@"6-------页面加载失败时调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didReceiveServerRedirectForProvisionalNavigation
:
(
WKNavigation
*
)
navigation
{
NSLog
(
@"-------接收到服务器跳转请求之后调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didFailNavigation
:
(
null_unspecified WKNavigation
*
)
navigation withError
:
(
NSError
*
)
error
{
NSLog
(
@"----数据加载发生错误时调用"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView didReceiveAuthenticationChallenge
:
(
NSURLAuthenticationChallenge
*
)
challenge completionHandler
:
(
void
(
^
)
(
NSURLSessionAuthChallengeDisposition disposition
,
NSURLCredential
*
_Nullable credential
)
)
completionHandler
{
NSLog
(
@"----需要响应身份验证时调用 同样在block中需要传入用户身份凭证"
)
;
NSURLCredential
*
newCred
=
[
NSURLCredential credentialWithUser
:
@""
password
:
@""
persistence
:
NSURLCredentialPersistenceNone
]
;
[
[
challenge sender
]
useCredential
:
newCred forAuthenticationChallenge
:
challenge
]
;
completionHandler
(
NSURLSessionAuthChallengeUseCredential
,
newCred
)
;
-
(
void
)
webViewWebContentProcessDidTerminate
:
(
WKWebView
*
)
webView
{
NSLog
(
@"----------进程被终止时调用"
)
;
#
pragma
mark - WKUIDelegate代理方法
-
(
void
)
webView
:
(
WKWebView
*
)
webView runJavaScriptAlertPanelWithMessage
:
(
NSString
*
)
message initiatedByFrame
:
(
WKFrameInfo
*
)
frame completionHandler
:
(
void
(
^
)
(
void
)
)
completionHandler
{
NSLog
(
@"-------web界面中有弹出警告框时调用"
)
;
-
(
nullable WKWebView
*
)
webView
:
(
WKWebView
*
)
webView createWebViewWithConfiguration
:
(
WKWebViewConfiguration
*
)
configuration forNavigationAction
:
(
WKNavigationAction
*
)
navigationAction windowFeatures
:
(
WKWindowFeatures
*
)
windowFeatures
{
NSLog
(
@"-----创建新的webView时调用的方法"
)
;
return
webView
;
-
(
void
)
webViewDidClose
:
(
WKWebView
*
)
webView
{
NSLog
(
@"----关闭webView时调用的方法"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView runJavaScriptConfirmPanelWithMessage
:
(
NSString
*
)
message initiatedByFrame
:
(
WKFrameInfo
*
)
frame completionHandler
:
(
void
(
^
)
(
BOOL
)
)
completionHandler
{
NSLog
(
@"%@"
,
message
)
;
completionHandler
(
YES
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView runJavaScriptTextInputPanelWithPrompt
:
(
NSString
*
)
prompt defaultText
:
(
NSString
*
)
defaultText initiatedByFrame
:
(
WKFrameInfo
*
)
frame completionHandler
:
(
void
(
^
)
(
NSString
*
_Nullable
)
)
completionHandler
{
NSLog
(
@"%@"
,
prompt
)
;
completionHandler
(
@"123"
)
;
-
(
void
)
webView
:
(
WKWebView
*
)
webView runOpenPanelWithParameters
:
(
WKOpenPanelParameters
*
)
parameters initiatedByFrame
:
(
WKFrameInfo
*
)
frame completionHandler
:
(
void
(
^
)
(
NSArray
<
NSURL
*
>
*
_Nullable URLs
)
)
completionHandler
{
NSLog
(
@"----显示一个文件上传面板"
)
;
-
(
WKWebView
*
)
webView
{
if
(
!
_webView
)
{
//简单使用,直接加载url地址
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_webView];
WKUserContentController
*
userContent
=
[
[
WKUserContentController alloc
]
init
]
;
[
userContent addScriptMessageHandler
:
self
name
:
@"NativeMethod"
]
;
WKWebViewConfiguration
*
config
=
[
[
WKWebViewConfiguration alloc
]
init
]
;
config
.
userContentController
=
userContent
;
_webView
=
[
[
WKWebView alloc
]
initWithFrame
:
self
.
view
.
bounds configuration
:
config
]
;
_webView
.
navigationDelegate
=
self
;
_webView
.
UIDelegate
=
self
;
[
self
.
view addSubview
:
_webView
]
;
return
_webView
;
demo:
https://github.com/ITHanYong/WKWebView.git
#import "ViewController.h"#import <WebKit/WebKit.h>@interface ViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>@property (nonatomic, strong) WKWebView *webView;@end@implementation ViewController- (void)viewD
Xcode8发布以后,编译器开始不支持
IOS
7,所以很多
应用
在适配
IOS
10之后都不在适配
IOS
7了,其中包括了很多大公司,网易新闻,滴滴出行等。因此,我们公司的
应用
也打算淘汰
IOS
7。
支持到
IOS
8,第一个要改的自然是用
WKWebView
替换原来的UIWebView。
WKWebView
有很多明显优势:
更多的支持
HTML5
的特性
官方宣称的高达60fps的滚动刷新率以及内置手势
将UIWebViewDelegate与UIWebView拆分成了14类与...
一、Cookie 概述
在浏览内核加载网络资源的过程中,往往离不开 HTTP 协议,它是在 Web 上进行数据交换的基础,同时也是一种无状态的 client-server 协议,这种无状态的属性促使许多端存储技术产生,其中最重要的技术之一就是 cookie 存储技术,它能方便的将数据存储于客户端,且在每次请求中都会在请求头中携带 cookie 数据并发送给 server。
cookie 技术的便捷性使得它在多种场景中被广泛使用,有时候甚至存在滥用情况,对同一 cookie 实例,
前端
、客户端、服务端都可以
self.webview = [[
WKWebView
alloc]initWithFrame:CGRectMake(0, 60, screen.size.width, screen.size.height)];
[self.view addSubview:self.webview];
2,加载百度Url
NSURL * url = [NSURL URLWithString: @"https
本demo是
WKWebView
的基本使用和交互 ,实现了原生调用js的方法、js调用原生的方法、通过拦截进行交互的方法;修改内容 加入沙盒 /
/加载沙盒 不带参数
// NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString * path = [paths objectAtIndex:0];
// path = [path stringByAppendingString:[NSString stringWithFormat:@"/app/html/index.html"]];
// NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"file://%@",path] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL:[NSURL fileURLWithPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject]];
// [self.wkView loadFileURL:url allowingReadAccessToURL:[NSURL fileURLWithPath: [paths objectAtIndex:0]]];
// 带参数
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * path = [paths objectAtIndex:0];
path = [path stringByAppendingString:[NSString stringWithFormat:@"/app/html/index.html"]];
NSURL * url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
[queryItemArray addObject:[NSURLQueryItem queryItemWithName:@"version" value:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]];
[urlComponents setQueryItems:queryItemArray];
[self.wkView loadFileURL:urlComponents.URL allowingReadAccessToURL:[NSURL fileURLWithPath: [paths objectAtIndex:0]]];
WKWebView
适配中最麻烦的就是cookie同步问题
WKWebView
采用了独立存储控件,因此和以往的UIWebView并不互通
虽然
iOS
11以后,
iOS
开放了WKHTTPCookieStore让开发者去同步,但是还是需要考虑低版本的 同步问题,本章节从各个角度切入考虑cookie同步问题
2.同步cookie(NSHTTPCookieStorage->WKHTTPCookieStore)
iOS
11+
可以直接使用WKHTTPCookieStore遍历方式设值,可以在创建
wkwebview
时候就同步也可以是请求时候
//
iOS
11同步 HTT
将UIWebVIew和
WKWebView
封装到一起,当系统版本大于8.0时候选择
WKWebView
降低性能消耗,当小于8.0时候使用UIWebView进行加载
1、将项目中的根目录中的“ZLCWebView源文件”中的ZLCWebView.h及m拖入工程(或直接在项目中拖出)
2、在自己的目标视图加载即可
//在目标视图内初始化ZLCWebView
ZLCWebView *my = [[ZLCWebView alloc]initWithFrame:self.view.bounds];
[my loadURLString:@"http://www.baidu.com"];
my.delegate = self;
[self.view addSubview:my];
//让视图遵守ZLCWebView的delegate并实现ZLCWebView的delegate
- (void)zlcwebViewDidStartLoad:(ZLCWebView *)webview
NSLog(@"页面开始加载");
- (void)zlcwebView:(ZLCWebView *)webview shouldStartLoadWithURL:(NSURL *)URL
NSLog(@"截取到URL:%@",URL);
- (void)zlcwebView:(ZLCWebView *)webview didFinishLoadingURL:(NSURL *)URL
NSLog(@"页面加载完成");
- (void)zlcwebView:(ZLCWebView *)webview didFailToLoadURL:(NSURL *)URL error:(NSError *)error
NSLog(@"加载出现错误");