对于之前使用的UIWebView,由于性能方面一直被吐槽,所以苹果官方现在推出一款性能极佳的WebKit库来代替UIWebView,WKWebView使用的内存是原来的1/3甚至1/4,WKWebView是现代WebKit API 在iOS8和OS X Yosemite应用中的核心部分。它代替了UIKit中的UIWebView和AppKit 中的WebView,提供了统一的跨双平台 API。
自诩拥有60fps滚动刷新率、内置手势、高效的app和web信息交换通道、和 Safari相同的JavaScript引擎,所以必须学习一下
初始化
initWithFrame:configuration:
init(frame frame: CGRect,
configuration configuration: WKWebViewConfiguration)
- (instancetype)initWithFrame:(CGRect)frame
configuration:(WKWebViewConfiguration *)configuration
** 这个方法可以自定义WKWebViewConfiguration。并初始化复制指定的配置。 如果你不想配置相关参数,可以使用initwithframe:初始默认配置实例方法。 **
属性介绍
- configuration:浏览器配置(只读)
@NSCopying var configuration: WKWebViewConfiguration { get }
@property(nonatomic, readonly, copy) WKWebViewConfiguration *configuration
- scrollview: 滚动视图(只读)
var scrollView: UIScrollView { get }
@property(nonatomic, readonly, strong) UIScrollView *scrollView
- title:页面的标题(只读))
var title: String? { get }
@property(nonatomic, readonly, copy) NSString *title
- URL:当前页面的URL(只读)
@NSCopying var URL: NSURL? { get }
@property(nonatomic, readonly, copy) NSURL *URL
- customUserAgent:用户设置的代理信息,没有为nil(9.0 and later)
var customUserAgent: String?
@property(nonatomic, copy) NSString *customUserAgent
- ertificateChain:当前已提交的导航的证书链的对象数组。(只读)(9.0 and later)
var certificateChain: [AnyObject] { get }
@property(nonatomic, readonly, copy) NSArray *certificateChain
- navigationDelegate:浏览器导航代理
weak var navigationDelegate: WKNavigationDelegate?
@property(nonatomic, weak) id< WKNavigationDelegate > navigationDelegate
- UIDelegate:浏览器用户界面代理
weak var UIDelegate: WKUIDelegate?
@property(nonatomic, weak) id< WKUIDelegate > UIDelegate
加载内容
- estimatedProgress:加载的进度值,从0到1
var estimatedProgress: Double { get }
@property(nonatomic, readonly) double estimatedProgress
- hasOnlySecureContent:是否安全加密加载页面(只读)
var hasOnlySecureContent: Bool { get }
@property(nonatomic, readonly) BOOL hasOnlySecureContent
- 加载HTML字符串
func loadHTMLString(_ string: String,
baseURL baseURL: NSURL?) -> WKNavigation?
- (WKNavigation *)loadHTMLString:(NSString *)string
baseURL:(NSURL *)baseURL
** Parameters
string HTML内容字符串
baseURL:解析HTML内容字符串里面的网址 **
- loading:是否正在加载中(只读)
var loading: Bool { get }
@property(nonatomic, readonly, getter=isLoading) BOOL loading
- reload:重新加载页面方法
func reload() -> WKNavigation?
- (WKNavigation *)reload
- reloadFromOrigin重新载入当前页面,进行端到端的验证使用缓存验证条件
func reloadFromOrigin() -> WKNavigation?
- (WKNavigation *)reloadFromOrigin
- stopLoading在当前页面停止所有的加载
func stopLoading()
- (void)stopLoading
- loadData:MIMEType:characterEncodingName:baseURL:加载数据
func loadData(_ data: NSData,
MIMEType MIMEType: String,
characterEncodingName characterEncodingName: String,
baseURL baseURL: NSURL) -> WKNavigation?
- (WKNavigation *)loadData:(NSData *)data
MIMEType:(NSString *)MIMEType
characterEncodingName:(NSString *)characterEncodingName
baseURL:(NSURL *)baseURL
** data: 网页转换的数据
MIMEType
{
application/msexcel
application/mshelp
application/mspowerpoint
application/msword
application/octet-stream
application/pdf
application/post
application/rtf
application/x-httpd-php
}
characterEncodingName :编码类型
baseURL:用于解析文档中的相对网址的网址。 **
- loadFileURL:allowingReadAccessToURL:加载导航到所请求的文件系统上的文件的URL
func loadFileURL(_ URL: NSURL,
allowingReadAccessToURL readAccessURL: NSURL) -> WKNavigation?
(WKNavigation *)loadFileURL:(NSURL *)URL
allowingReadAccessToURL:(NSURL *)readAccessURL
** readAccessURL如果是一个文件,直接加载,如果是一个目录,加载里面的文件 **
Navigating导航
- allowsBackForwardNavigationGestures是否允许水平滑动前进后退页面。默认是NO
var allowsBackForwardNavigationGestures: Bool
allowsBackForwardNavigationGestures
- backForwardList历史记录
ar backForwardList: WKBackForwardList { get }
@property(nonatomic, readonly, strong) WKBackForwardList *backForwardList
- canGoBack:是否后退到历史记录的一项(只读)
var canGoBack: Bool { get }
@property(nonatomic, readonly) BOOL canGoBack
- canGoForward:是否前进到历史记录的某一项(只读)
var canGoForward: Bool { get }
@property(nonatomic, readonly) BOOL canGoForward
- allowsLinkPreview:是否支持链接预览,支持3DTouch查看等
var allowsLinkPreview: Bool
@property(nonatomic) BOOL allowsLinkPreview
- goBack:回到后一页
func goBack() -> WKNavigation?
- (WKNavigation *)goBack
- oForward:前进一页
func goForward() -> WKNavigation?
- (WKNavigation *)goForward
- goToBackForwardListItem:导航到一个新的项目(从后向前),并设置为当前项目
func goToBackForwardListItem(_ item: WKBackForwardListItem) -> WKNavigation?
- (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item
- loadRequest:加载URL的请求
func loadRequest(_ request: NSURLRequest) -> WKNavigation?
- (WKNavigation *)loadRequest:(NSURLRequest *)request
- 执行JavaScript语言
func evaluateJavaScript(_ javaScriptString: String,
completionHandler completionHandler: ((AnyObject?, NSError?) -> Void)?)
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *error))completionHandler
** completionHandler:执行后的block回调 **