{"id":295,"date":"2025-07-31T14:54:05","date_gmt":"2025-07-31T06:54:05","guid":{"rendered":"https:\/\/www.nickyoo7.com\/?p=295"},"modified":"2025-07-31T14:54:05","modified_gmt":"2025-07-31T06:54:05","slug":"xcode-16-4-%e4%bd%bf%e7%94%a8-webview","status":"publish","type":"post","link":"https:\/\/www.nickyoo7.com\/?p=295","title":{"rendered":"xcode 16.4 \u4f7f\u7528 webview"},"content":{"rendered":"<p>xcode 16.4 \u4f7f\u7528 webview<br \/>\n<!--more-->01.\u65b0\u589e\u5c08\u6848 :<br \/>\ntemplate \u9078 iOS -&gt; App \uff0cInterface \u9078 Storyboard \uff0cLanguage \u9078 swift<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-296\" src=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.46.46.png\" alt=\"\" width=\"730\" height=\"520\" srcset=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.46.46-300x214.png 300w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.46.46.png 730w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-297\" src=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.47.48.png\" alt=\"\" width=\"732\" height=\"519\" srcset=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.47.48-300x214.png 300w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2025\/07\/\u622a\u5716-2025-07-31-\u4e2d\u534812.47.48.png 732w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>02.\u7de8\u8f2f ViewController.swift<br \/>\n(1) \u65b0\u589e import WebKit<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">import WebKit<\/pre>\n<p>(2)\u5728 class ViewController: UIViewController { }\u5167\u65b0\u589e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">var webView: WKWebView!\r\n\r\noverride func loadView() {\r\n    let prefs=WKWebpagePreferences();\r\n    prefs.allowsContentJavaScript = true\r\n    \r\n    let webConfiguration = WKWebViewConfiguration()\r\n    webConfiguration.defaultWebpagePreferences = prefs\r\n    \r\n    \r\n    webView = WKWebView(frame: .zero, configuration: webConfiguration)\r\n    webView.navigationDelegate = self\r\n    view = webView\r\n}<\/pre>\n<p>(3) \u5728 override func viewDidLoad() {}\u00a0 super.viewDidLoad() \u7af9\u4eba\u5973\u6208\u7530\u5f8c\u65b0\u589e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">let myURL = URL(string:\"https:\/\/tw.yahoo.com )\r\nlet myRequest = URLRequest(url: myURL!)\r\nwebView.load(myRequest)\r\nwebView.allowsBackForwardNavigationGestures = true<\/pre>\n<p>03.\u8655\u7406\u7576\u7db2\u9801\u4e2d\u7684\u9023\u7d50 target=&#8221;_blank&#8221; \u7121\u6cd5\u986f\u793a\u7684\u554f\u984c<br \/>\n(01) \u5728ViewController.swift \u65b0\u589e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">extension ViewController: WKNavigationDelegate {\r\n    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {\r\n        \r\n        if navigationAction.targetFrame == nil {\r\n            decisionHandler(.cancel)\r\n            webView.load(navigationAction.request)\r\n            return\r\n        }\r\n\r\n        decisionHandler(.allow)\r\n    }\r\n    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -&gt; WKWebView? {\r\n        \/\/ Handle creation of new windows (e.g., for pop-ups)\r\n        return nil \/\/ Or return a new WKWebView for the pop-up\r\n    }\r\n}\r\n<\/pre>\n<p>(02) \u5728 override func loadView() \u5167 \u7684 view = webView \u65b0\u589e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">webView.navigationDelegate = self<\/pre>\n<p>(03) \u5982\u679c\u8981\u4f7f\u7528\u5916\u90e8\u700f\u89bd\u5668\u6253\u958b\uff0c\u5247<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">\/\/ \u70ba\u6c42\u7bc4\u4f8b\u55ae\u7d14\u5316\u9019\u908a\u53ea\u6aa2\u67e5 targetFrame \uff0c\r\n\/\/ \u5be6\u52d9\u4e0a\u9084\u662f\u9700\u8981\u52a0\u4e0a\u6aa2\u67e5 url \u662f\u4e0d\u662f\u5408\u6cd5\u53ef\u4ee5\u958b\u555f\u7b49\u7b49\u8655\u7406\r\n  if navigationAction.targetFrame == nil {\r\n      decisionHandler(.cancel)\r\n      UIApplication.shared.open( navigationAction.request )\r\n      return\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53c3\u8003\u8cc7\u6599 :<br \/>\n1.https:\/\/qiita.com\/vc7\/items\/90bd7da89aee54936bfd<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>xcode 16.4 \u4f7f\u7528 webview<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-ios","entry"],"_links":{"self":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295"}],"version-history":[{"count":1,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":298,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/295\/revisions\/298"}],"wp:attachment":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}