{"id":299,"date":"2025-07-31T17:40:44","date_gmt":"2025-07-31T09:40:44","guid":{"rendered":"https:\/\/www.nickyoo7.com\/?p=299"},"modified":"2025-07-31T17:40:44","modified_gmt":"2025-07-31T09:40:44","slug":"webview-%e8%88%87app%e4%ba%92%e5%82%b3%e8%b3%87%e8%a8%8a","status":"publish","type":"post","link":"https:\/\/www.nickyoo7.com\/?p=299","title":{"rendered":"WebView  \u8207App\u4e92\u50b3\u8cc7\u8a0a"},"content":{"rendered":"<p>WebView \u8207App\u4e92\u50b3\u8cc7\u8a0a<br \/>\n<!--more-->01.\u7de8\u8f2f ViewControl.swift<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">\u5728class ViewController: UIViewController,\u52a0\u5165 WKUIDelegate, WKScriptMessageHandler \r\n\r\n\u8b8a\u6210 class ViewController: UIViewController,WKUIDelegate, WKScriptMessageHandler {<\/pre>\n<p>\u5728class ViewController: UIViewController,WKUIDelegate, WKScriptMessageHandler {} \u4e2d\u65b0\u589e<br \/>\n\u9019\u5340\u6bb5\u8655\u7406\u6bcf\u4e00\u500b\u5c0d\u61c9\u7684\u4f5c\u696d<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {\r\n    switch message.name {\r\n    case \"openURL\":\r\n        let url01 = URL(string: message.body as! String)!\r\n        UIApplication.shared.open(url01)\r\n    case \"shareLine\":\r\n        let l_str = message.body  as! String\r\n        let u_str = l_str.components(separatedBy: \"?url=\")\r\n        let lineURL=URL(string: \"line:\/\/msg\/text\/\" + u_str[1])\r\n        if UIApplication.shared.canOpenURL( lineURL! ) {\r\n            UIApplication.shared.open(lineURL!, options: [:],completionHandler: nil)\r\n        }else{\r\n            guard let url01 = URL(string: message.body  as! String) else {return}\r\n            UIApplication.shared.open(url01)\r\n        }\r\n    default:\r\n            break\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>02.override func loadView() {} \u5340\u6bb5\u4e2d\u52a0\u5165<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">    override func loadView() {\r\n        let prefs=WKWebpagePreferences();\r\n        prefs.allowsContentJavaScript = true \/\/ios 14 \u5f8c\u624d\u6709\u652f\u63f4\r\n        \r\n        let webConfiguration = WKWebViewConfiguration()\r\n        webConfiguration.defaultWebpagePreferences = prefs\r\n        \/\/\u4ee5\u4e0b\u70ba\u8a3b\u518a\u8207\u524d\u7aef\u7db2\u9801\u6e9d\u901a\u7528 Func\r\n        \/\/\u9700\u8981\u7528\u5230\u7684\u90fd\u8981\u5728\u9019\u5148\u8a3b\u518a\r\n        webConfiguration.userContentController = WKUserContentController()\r\n        webConfiguration.userContentController.add(self ,name : \"openURL\")\r\n        webConfiguration.userContentController.add(self ,name : \"shareLine\")\r\n        webConfiguration.userContentController.add(self ,name : \"shareFB\")\r\n        webConfiguration.userContentController.add(self ,name : \"setBadge\")\r\n\r\n        webView = WKWebView(frame: .zero, configuration: webConfiguration)\r\n\r\n        view = webView\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebView \u8207App\u4e92\u50b3\u8cc7\u8a0a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/299","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=299"}],"version-history":[{"count":1,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/300"}],"wp:attachment":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}