{"id":763,"date":"2025-06-02T11:17:21","date_gmt":"2025-06-05T03:36:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=763"},"modified":"2025-06-05T11:17:21","modified_gmt":"2025-06-05T03:36:21","slug":"swift-wkwebview-%e8%ae%80%e5%8f%96%e7%b6%b2%e5%9d%80%e3%80%81%e9%80%a3%e7%b5%90%e3%80%81url","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-wkwebview-%e8%ae%80%e5%8f%96%e7%b6%b2%e5%9d%80%e3%80%81%e9%80%a3%e7%b5%90%e3%80%81url\/","title":{"rendered":"2025 \u6700\u65b0 Swift WKWebView \u4f7f\u7528\u6559\u5b78\uff1a\u8b80\u53d6\u7db2\u5740\u8207\u64cd\u4f5c\u6280\u5de7"},"content":{"rendered":"<p>&#8220;`swift<br \/>\nimport WebKit<\/p>\n<p>\/\/ \u5275\u5efa WKWebView \u5be6\u4f8b<br \/>\nlet webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))<br \/>\nwebView.uiDelegate = self \/\/ \u8a2d\u5b9a UI \u4ee3\u7406<br \/>\nself.view.addSubview(webView) \/\/ \u5c07 webView \u52a0\u5165\u8996\u5716<\/p>\n<p>\/\/ \u8f09\u5165\u7db2\u5740<br \/>\nif let url = URL(string: &#8220;https:\/\/www.google.com\/&#8221;) {<br \/>\n    webView.load(URLRequest(url: url))<br \/>\n} else {<br \/>\n    print(&#8220;\u7121\u6cd5\u5275\u5efa URL.&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### \u57fa\u672c\u64cd\u4f5c<br \/>\n\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 WKWebView \u64cd\u4f5c\u65b9\u6cd5\uff1a<\/p>\n<p>&#8211; **\u4e0a\u4e00\u9801**<br \/>\n  &#8220;`swift<br \/>\n  webView.goBack()<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u4e0b\u4e00\u9801**<br \/>\n  &#8220;`swift<br \/>\n  webView.goForward()<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u91cd\u65b0\u6574\u7406**<br \/>\n  &#8220;`swift<br \/>\n  webView.reload()<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u505c\u6b62\u91cd\u65b0\u6574\u7406**<br \/>\n  &#8220;`swift<br \/>\n  webView.stopLoading()<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u6aa2\u67e5\u662f\u5426\u53ef\u4ee5\u4e0a\u4e00\u9801**<br \/>\n  &#8220;`swift<br \/>\n  if webView.canGoBack {<br \/>\n      \/\/ \u9032\u884c\u4e0a\u4e00\u9801\u64cd\u4f5c<br \/>\n  }<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u6aa2\u67e5\u662f\u5426\u53ef\u4ee5\u4e0b\u4e00\u9801**<br \/>\n  &#8220;`swift<br \/>\n  if webView.canGoForward {<br \/>\n      \/\/ \u9032\u884c\u4e0b\u4e00\u9801\u64cd\u4f5c<br \/>\n  }<br \/>\n  &#8220;`<\/p>\n<p>&#8211; **\u6aa2\u67e5\u662f\u5426\u6b63\u5728\u52a0\u8f09**<br \/>\n  &#8220;`swift<br \/>\n  if webView.isLoading {<br \/>\n      \/\/ \u986f\u793a\u52a0\u8f09\u6307\u793a<br \/>\n  }<br \/>\n  &#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<br \/>\n\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5982 URL \u683c\u5f0f\u932f\u8aa4\u6216\u7db2\u7d61\u9023\u63a5\u554f\u984c\u3002\u5efa\u8b70\u5728\u8f09\u5165 URL \u524d\u9032\u884c\u6aa2\u67e5\uff0c\u4e26\u5728\u767c\u751f\u932f\u8aa4\u6642\u63d0\u4f9b\u76f8\u61c9\u7684\u932f\u8aa4\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<br \/>\nWKWebView \u4e0d\u50c5\u53ef\u4ee5\u8f09\u5165\u7db2\u9801\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u986f\u793a\u672c\u5730 HTML \u6587\u4ef6\u3001JavaScript \u57f7\u884c\u4ee5\u53ca\u8207 iOS \u61c9\u7528\u7684\u4e92\u52d5\u3002\u9019\u4f7f\u5f97 WKWebView \u6210\u70ba\u958b\u767c\u5bcc\u4e92\u52d5\u6027\u61c9\u7528\u7684\u5f37\u5927\u5de5\u5177\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/12\/Dec-11-2020-10-26-55.gif\" alt=\"WKWebView \u793a\u4f8b\" title=\"\"><\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<br \/>\n**Q1: \u5982\u4f55\u5728 WKWebView \u4e2d\u958b\u555f\u65b0\u9023\u7d50\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u5be6\u73fe `WKNavigationDelegate` \u4e26\u5728 `webView(_:decidePolicyFor:decisionHandler:)` \u4e2d\u8655\u7406\u65b0\u9023\u7d50\u7684\u958b\u555f\u884c\u70ba\u3002<\/p>\n<p>**Q2: WKWebView \u652f\u6301\u54ea\u4e9b\u7db2\u9801\u529f\u80fd\uff1f**<br \/>\nA2: WKWebView \u652f\u6301 JavaScript\u3001HTML5\u3001CSS3 \u7b49\u591a\u7a2e\u7db2\u9801\u6280\u8853\uff0c\u4e26\u80fd\u5920\u6e32\u67d3\u591a\u5a92\u9ad4\u5167\u5bb9\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728 WKWebView \u4e2d\u57f7\u884c JavaScript\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u4f7f\u7528 `evaluateJavaScript(_:completionHandler:)` \u65b9\u6cd5\u4f86\u57f7\u884c JavaScript \u4ee3\u78bc\u4e26\u7372\u5f97\u7d50\u679c\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u4e0d\u50c5\u66f4\u65b0\u4e86\u904e\u6642\u7684\u8a9e\u6cd5\uff0c\u9084\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u5be6\u7528\u8cc7\u8a0a\uff0c\u4e26\u7b26\u5408 SEO \u6700\u4f73\u5be6\u8e10\u3002\u5e0c\u671b\u9019\u80fd\u5e6b\u52a9\u60a8\u63d0\u5347\u6587\u7ae0\u7684\u8cea\u91cf\u8207\u53ef\u8b80\u6027\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`swift import WebK&#8230;<\/p>\n","protected":false},"author":1,"featured_media":760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-763","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/12\/Dec-11-2020-10-26-55.gif","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-cj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=763"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/763\/revisions"}],"predecessor-version":[{"id":988,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/763\/revisions\/988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/760"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}