WKWebView 官方文档翻译

WKWebView 类

一个WKWebView对象可以显示交互式的web内容.就像一个应用程序的浏览器.你可以使用WKWebView类嵌入Web内容的应用程序.这样做,创造一个WKWebView对象,设置为它的view.并发送一个请求来加载网页的内容

一.Overview 概述

重要提示:

在 iOS 8.0 和 OS X 10.10 之后,使用WKWebView来加载网页内容到你的App,不要再使用 UIWebViewWebView

1.创建一个新的WKWebView

下面的方法是一个初始化方法,可以通过WKWebViewConfiguration来自定义配置.只能在初始化的时候进行配置,初始化之后修改configuration对web视图没有影响.你也可以使用initWithframe:方法,这个方法使用的是默认的配置.

// 参数1 : frame: 新webView 的frame
// 参数2:  configuration : 新webView 的配置
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration;
2.加载网页内容.

[1]加载本地 HTML 文件

// string : 作为网页内容使用的字符串
// baseURL : 解析文档中相对URL的URL
- (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;

[2]加载web内容

// request : 指定导航到的URL的请求
- (WKNavigation *)loadRequest:(NSURLRequest *)request;
3.停止加载当前页上的所有资源
- (void)stopLoading;
4.确定web视图是否处于加载的过程中.

如果正在加载内容,则设置为ture.否则false.WKWebView 可以通过 KVO 来监听此属性的变化.

@property (nonatomic, readonly, getter=isLoading) BOOL loading;
5.通过 WKUIDelegate 代理属性来监听网络内容的加载
6.在历史记录中向后导航.
- (WKNavigation *)goBack;
7.向前导航
- (WKNavigation *)goForward;
8.确定是否可以向前还是向后的属性
@property (nonatomic, readonly) BOOL canGoBack;
@property (nonatomic, readonly) BOOL canGoForward;
9.链接时数据检测类型

因为在默认的情况下,一个 webView 中会自动将出现在 web 内容种电话号码转换为电话链接.当一个电话链接被点击时,手机 App 开始拨号.那么为了关掉这一行为,需要设置 WKDataDetectorTypes 属性.

@property (nonatiomic) WKDataDetectorTypes dataDetectorTypes;
// 检测到的东西都转为对应的链接了
typedef enum WKDataDetectorTypes : NSUInteger {
    WKDataDetectorTypeNone = 0,
    WKDataDetectorTypePhoneNumber = 1 << 0,
    WKDataDetectorTypeLink = 1 << 1,
    WKDataDetectorTypeAddress = 1 << 2,
    WKDataDetectorTypeCalendarEvent = 1 << 3,
    WKDataDetectorTypeTrackingNumber = 1 << 4,
    WKDataDetectorTypeFlightNumber = 1 << 5,
    WKDataDetectorTypeLookupSuggestion = 1 << 6,
    WKDataDetectorTypeAll = NSUIntegerMax,
    WKDataDetectorTypeSpotlightSuggestion = WKDataDetectorTypeLookupSuggestion
} WKDataDetectorTypes;
10.用指定的因素对页面内容进行缩放,并将结果集中在指定的点上
// magnification : 缩放内容的因素
- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;

二.Symbols

1.初始化 webView

@property(nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration;
- (instancetype)initWithCoder:(NSCoder *)coder;

2.查看视图信息

1.与 webView 关联的 scrollView

@property(nonatomic, readonly, strong) UIScrollView *scrollView;

2.页面标题

WKWebView 可以通过 KVO 来观察此属性

@property (nonatomic, readonly, copy) NSString *title;

3.活动网址

符合 KVO

@property(nonatomic, readonly, copy) NSURL *URL;

4.自定义用户代理字符串

@property(nonatomic, copy) NSString *customUserAgent;

5.当前提交导航的一个 SecTrustRef 对象

@property(nonatomic, readonly) SecTrustRef serverTrust;

3.设置代理

1.webView的导航代理

@property(nonatomic, weak) id<WKNavigationDelegate> navigationDelegate;

2.webview 的用户交互代理

@property(nonatomic, weak) id<WKUIDelegate> UIDelegate;

4.加载内容

1.估计当前导航加载的进度

@property(nonatomic, readonly) double estimatedProgress;

2.布尔值,页面上的所有资源是否已通过安全加密连接加载

@property(nonatomic, readonly) BOOL hasOnlySecureContent;
- (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;

4.布尔值,表示视图当前是否正在加载内容

@property(nonatomic, readonly, getter=isLoading) BOOL loading;

5.刷新当前页面

- (WKNavigation *)reload;

6.如果可能的话,通过使用缓存验证条件,端到端的验证来重新载入当前页面

- (WKNavigation *)reloadFromOrigin.

7.停止加载

- (void)stopLoading;
// 1.data : 网页内容使用的数据
// 2.MIMEType : 数据的MIME类型
- (WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL;

9.导航到所请求的文件系统上的文件的URL

- (WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL;

5.缩放内容

1.是否通过放大手势来改变webview 的放大倍率

@property(nonatomic) BOOL allowsMagnification;

2.当前页面内容的缩放因子.默认1.0

@property (nonatomic) CGFloat magnification;

3.用指定的因子对页面内容进行缩放.并将结果集中在指定的点上

- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;

6.导航

1.布尔值.是否可以通过水平滑动手势来操作前进或后退的导航

@property(nonatomic) BOOL allowsBackForwardNavigationGestures;

2.webview 的前向列表

@property(nonatomic, readonly, strong) WKBackForwardList *backForwardList;
@property(nonatomic, readonly) BOOL canGoBack;
@property(nonatomic, readonly) BOOL canGoForward;
// 决定链接是否可用
@property(nonatomic) BOOL allowsLinkPreview;
- (WKNavigation *)goBack;
- (WKNavigation *)goForward;

// 导航到一个项目从后向前列表并设置为当前项目
- (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;

// 导航到一个 URL
- (WKNavigation *)loadRequest:(NSURLRequest *)request;

7.执行 JavaScript

- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *error))completionHandler;

8.实例方法


// 1.
- (IBAction)goBack:(id)sender;

// 2.
- (IBAction)goForward:(id)sender;

// 3.
- (IBAction)reload:(id)sender;

// 4.
- (IBAction)reload:(id)sender;

// 5.
- (IBAction)stopLoading:(id)sender;

其他相关类说明

一.WKUIDelegate

1.创建一个新的webView

// 1.webView : 调用代理方法的webview
// 2.configuration : 创建新的webview 时用到的配置
// 3.navigationAction : 创建新web视图的导航操作
// 4.windowFeatures : 网页要求的窗口特性
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNanigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

2.显示用户UI界面

[1]显示一个 JavaScript警报面板

// 1.webview : 调用代理方法的webview
// 2.message : 要显示的message
// 3.frame   : 关于 JavaScript 进程发起此嗲偶偶那个的框架的信息.
// 4.completionHandler : 警报面板调用完成之后的回调 
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;

注意:

对与用户安全来讲,实现这个方法要注意到一个事实,就是一个特定的网站控制这个警报面板的内容.识别控制网站的最简单的方法是用frame.request.URL.host.这个警报面板应该有一个OK按钮.

[2]显示一个 JavaScript 确认面板

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;

[3]显示一个 JavaScript 文本输入面板

// 1.prompt : 要显示的消息
// 2.defaultText : 要显示的初始文本
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler;

3.关闭web视图

通知App成功关闭DOM窗口

- (void)webViewDidClose:(WKWebView *)webView;

注意:

应用程序要从视图结构中将 web 视图删除.并且更新UI.如关闭包含的浏览器选项卡或窗口

4.显示上传面板

// 1.parameters : 描述文件上传控件的参数
- (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> *URLs))completionHandler;

注意:

如果没有实现此方法,webView 就会表现出像用户选择取消按钮一样.

5.Responding to Force Touch Actions

[1]确定是否应该显示给定的元素

- (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo;

[2]当用户执行 a peek action(一瞥?偷看?)时调用

- (UIViewController *)webView:(WKWebView *)webView previewingViewControllerForElement:(WKPreviewElementInfo *)elementInfo defaultActions:(NSArray<id<WKPreviewActionItem>> *)previewActions;

[3]当用户在预览时执行弹出动作时调用

- (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController;

二.WKNavigationDelegate

当你在web视图的接受过程中,加载,完成导航请求时,WKNavigationDelegate 协议的方法帮助你实现自定义行为,

1.初始化 Navigation

当webView 开始接收web内容时调用

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;

当web内容开始在一个webview加载时调用

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;

2.响应服务器操作

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;

2.当webView接收服务器重定向时调用

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;

3.反应错误

1.导航期间发生错误时调用

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error;

2.webView 加载内容时发生错误时调用

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error;

4.跟踪负荷的进度

1.导航完成时调用

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;

2.当webview 的web内容进程终止的时候调用

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;

5.允许导航

1.决定是否允许或取消导航

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

2.决定是否在其响应已知后允许导航或取消导航

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

6.导航策略

typedef enum WKNavigationActionPolicy : NSInteger {
    WKNavigationActionPolicyCancel,
    WKNavigationActionPolicyAllow
} WKNavigationActionPolicy;

7.实例方法

当 web视图需要响应身份验证挑战时调用

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler;

三.WKWebViewConfiguration 类

用于初始化 Web 视图的属性集合


一.Overview 概述

使用 WKWebViewConfiguration 这个类,你可以决定如何尽快的来呈现一个网页.它有许多属性可以设置,媒体播放,用户可选择项的粒度,和许多其他的选择.

WKWebViewConfiguration 这个类,是只有当一个 Web 视图在初始化的时候,才能被使用.在我们创建了 Web 视图之后,不能使用这个类来更改其配置了.


二.Symbols 标志

Configuring the New Web View's Properties 配置新Web视图的属性

1.applicationNameForUserAgent

在用户代理字符串中使用的应用程序名称

@property (nonatomic, copy) NSString *applicationNameForUserAgent;

2.preferences

web 视图使用的首选项

@property (nonatomic, copy) WKPreferences *preferences

3.processPool

获取视图web内容过程的流程池

当Web视图初始化时,要么为指定的池创建一个新的web内容流程,要么使用该池中的现有进程

@property(nonatomic, strong) WKProcessPool *processPool;

4.userContentController

与web视图关联的用户内容控制器

@property(nonatomic, strong) WKUserContentController *userContentController;

5.websiteDataStore

web视图所使用的网站数据存储

如果Web视图和一个非持久性数据存储相关联,没有数据被写入文件系统,此属性实现Web视图中的私有浏览.

@property(nonatomic, strong) WKUserContentController *userContentController;

Determining Webpage Scalability 确定网页的扩展性

1.ignoresViewportScaleLimits

一个 布尔值,确定 wkwebView对象是否总是允许网页缩放

将此属性设置为TRUE,可以使网页进行缩放,而不管作者的意图如何.默认是FALSE

@property(nonatomic) BOOL ignoresViewportScaleLimits;

Setting Rendering Preferences 设置渲染参数

1.suppressesIncrementalRendering

一个布尔值,指示 web 视图是否在完全加载到内存中之前禁止内容呈现

默认是NO

@property(nonatomic) BOOL suppressesIncrementalRendering;

Setting Media Playback Preferences 设置媒体播放首选项

1.allowsInlineMediaPlayback

一个布尔值,指示 HTML5 视频是否在线播放或使用本机全屏控制器

你必须将此属性设置为播放(inline video)内联视频.设置这个属性为true来播放在线视频.设置这个属性为false来使用原生的全屏控制器来播放视频.当添加一个HTML视频文件到iPhone上的时候,你必须包含playsinline属性.

这个属性对于iPhone来说,默认是false,对于iPad来说,默认是true.

注意: 在 iOS 10.0 之后必须使用webkit-playsinline属性

@property (nonatomic) BOOL allowsInlineMediaPlayback;

2.allowsAirPlayForMediaPlayback

一个布尔值,指示 AirPlay 是否可用.默认YES.

@property(nonatomic) BOOL allowsAirPlayForMediaPlayback;

3.allowsPictureInPictureMediaPlayback

一个布尔值,指示HTML5视频是否支持画中画,默认YES.

@property (nonatomic) BOOL allowsPictureInPictureMediaPlayback;

4.mediaTypesRequiringUserActionForPlayback

确定哪些视频类型需要用户手势开始播放

@property (nonatomic) WKAudiovisualMediaTypes mediaTypesRequiringUserActionForPlayback;

5.mediaPlaybackAllowsAirPlay[过期方法]

  • 过期!用allowsAirPlayForMediaPlayback方法替代.

6.requiresUserActionForMediaPlayback[过期方法]

  • 布尔值,指示 HTML5 视频是否需要用户控制播放还是视频自动播放

7.mediaPlaybackRequiresUserAction[过期方法]

  • mediaTypesRequiringUserActionForPlayback替代

8.WKAudiovisualMediaTypes

需要通过用户的手势才开始播放的媒体类型

typedef enum WKAudiovisualMediaTypes : NSUInteger {
		WKAudiovisualMediaTypeNone  = 0,
		WKAudiovisualMediaTypeAudio = 1 << 0,
		WKAudiovisualMediaTypeVideo = 1 << 1,
		WKAudiovisualMediaTypeAll   = NSUIntegerMax 
} WKAudiovisualMediaTypes;

Setting Selection Granularity 设置选择粒度

1.selectionGranularity

用户可以交互选择web视图中的内容的粒度级别.默认是WKSelectionGranularityDynamic

@property (nonatomic) WKSelectionGranularuty selectionGranularity;

2.WKSelectionGranularity

可以以交互的方式创建和修改选择的粒度

typedef enum WKSelectionGranularity : NSInteger {
	WKSelectionGranuarityDynamic,
	WKSelectionGranularityCharacter
} WKSelectionGranularity;

Selecting User Interface Directionality 选择用户界面方向性

1.userInterfaceDirectionPolicy

用户界面元素的方向性

@property(nonatomic) WKUserInterfaceDirectionPolicy userInterfaceDirectionPolicy;

2.WKUserInterfaceDirectionPolicy

用于确定web视图中用户界面元素方向性的策略

typedef enum WKUserInterfaceDirectionPolicy : NSInteger {
    WKUserInterfaceDirectionPolicyContent,
    WKUserInterfaceDirectionPolicySystem
} WKUserInterfaceDirectionPolicy;

Identifying Data Types 识别数据类型

1.dataDetectorTypes

所需的数据检测类型

@property(nonatomic) WKDataDetectorTypes dataDetectorTypes;

2.WKDataDetectorTypes

检测到的数据类型

typedef enum WKDataDetectorTypes : NSUInteger {
    WKDataDetectorTypeNone = 0,
    WKDataDetectorTypePhoneNumber = 1 << 0,
    WKDataDetectorTypeLink = 1 << 1,
    WKDataDetectorTypeAddress = 1 << 2,
    WKDataDetectorTypeCalendarEvent = 1 << 3,
    WKDataDetectorTypeTrackingNumber = 1 << 4,
    WKDataDetectorTypeFlightNumber = 1 << 5,
    WKDataDetectorTypeLookupSuggestion = 1 << 6,
    WKDataDetectorTypeAll = NSUIntegerMax,
    WKDataDetectorTypeSpotlightSuggestion = WKDataDetectorTypeLookupSuggestion
} WKDataDetectorTypes;

三.Relationships 关系


Inherits From 继承

NSObject


Conforms To 符合

NSCoding

NSCopying

posted @ 2017-06-16 12:46  Oo11  阅读(1498)  评论(0编辑  收藏  举报