# 220-Customized Loading in WKWebView
# Manage cookies
有以下特点:
- Add and remove individual cookies
- Access all cookies visible to a WKWebView
- Including HTTP-only cookies
- Observe the cookie store for changes
WKHTTPCookieStore
WKWebView.WKWebViewConfiguration.WKWebsiteDataStore.httpCookieStore
1
# Filter unwanted content
有以下特点:
- Block loads
- Make content invisible
- Make insecure loads secure
WKContentRuleListStore
WKContentRuleList
WKWebView.WKWebViewConfiguration.WKUserContentController.add()
WKWebView.WKWebViewConfiguration.WKUserContentController.remove()
WKWebView.WKWebViewConfiguration.WKUserContentController.removeAllContentRuleLists()
1
2
3
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
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
2
3
4
5
6
7