返回列表

# 220-Customized Loading in WKWebView

# Manage cookies

有以下特点:

  1. Add and remove individual cookies
  2. Access all cookies visible to a WKWebView
  3. Including HTTP-only cookies
  4. Observe the cookie store for changes

WKHTTPCookieStore

WKWebView.WKWebViewConfiguration.WKWebsiteDataStore.httpCookieStore
1

# Filter unwanted content

有以下特点:

  1. Block loads
  2. Make content invisible
  3. Make insecure loads secure

WKContentRuleListStore

WKContentRuleList

WKWebView.WKWebViewConfiguration.WKUserContentController.add()
WKWebView.WKWebViewConfiguration.WKUserContentController.remove()
WKWebView.WKWebViewConfiguration.WKUserContentController.removeAllContentRuleLists()
1
2
3

Safari Content-Blocking Rules (opens new window) 一样的语法

# Provide custom resources

Web的内容使用一个 custom 的 scheme, native app 响应 这个scheme

WKURLSchemeHandler

protocol WKURLSchemeHandler {
  func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask)
  func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask)	
}
1
2
3
4
WKWebView.WKWebViewConfiguration.setURLSchemeHandler(_ urlSchemeHandler: WKURLSchemeHandler?, forURLScheme urlScheme: String)
1

WKURLSchemeTask

protocol WKURLSchemeTask {
  var request: URLRequest { get }
  func didReceive(_ response: URLResponse)
  func didReceive(_ data: Data)
  func didFinish()
  func didFailWithError(_ error: Error)
}
1
2
3
4
5
6
7