{"id":4861,"date":"2022-01-06T15:28:30","date_gmt":"2022-01-06T07:28:30","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4861"},"modified":"2022-01-06T15:28:30","modified_gmt":"2022-01-06T07:28:30","slug":"swiftui-%e6%89%93%e5%bc%80-safari-or-default-browser-with-post","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4861","title":{"rendered":"SwiftUI: \u6253\u5f00 Safari (or default browser) with POST"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">\u524d\u8a00<\/h5>\n\n\n\n<p>\u5728Swift\u4e2d\uff0c\u7528Safari\u6253\u5f00URL\u6bd4\u8f83\u7b80\u5355\u3002\u4f46\u662f\u8fd9\u90fd\u662fget\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>\u5047\u5982\u6211\u4eec\u9700\u8981\u7528post\u8bf7\u6c42\u5148load Html\u9875\u9762\uff0c\u8be5\u5982\u4f55\u5904\u7406\u5462\uff1f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">     let url = URL(string: \"https:\/\/www.baidu.com\")\n        \/\/\u6839\u636eiOS\u7cfb\u7edf\u7248\u672c\uff0c\u5206\u522b\u5904\u7406\n        if #available(iOS 10, *) {\n            \/\/ get\u6253\u5f00\n            UIApplication.shared.open(url!, options: [:],\n                                      completionHandler: {(success) in\n                                        print(success)\n            })\n        } else {\n            \/\/ get\u6253\u5f00\n            UIApplication.shared.openURL(url!)\n        }\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h5>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7webView\u6765\u89e3\u51b3\uff0c\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>wkWebView post\u52a0\u8f7d\u7f51\u9875 <strong><span class=\"has-inline-color has-vivid-red-color\">&#8211;><\/span><\/strong> wkWebView\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u83b7\u53d6wkWebView\u7684url <strong><span class=\"has-inline-color has-vivid-red-color\">&#8211;><\/span><\/strong> \u901a\u8fc7\u6d4f\u89c8\u5668\u6253\u5f00url<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct WebView: UIViewRepresentable {\n    var url: String\n    var text: String\n    \n    func makeUIView(context: Context) -> WKWebView {\n        guard let url = URL(string: url) else {\n            return WKWebView()\n        }\n\n        \/\/ \u901a\u8fc7wkWebView Post loadHtml\u9875\u9762\u3002\n        let wkWebView = WKWebView()\n        wkWebView.navigationDelegate = context.coordinator\n        var request = URLRequest(url: url)\n        request.addValue(\"application\/json\", forHTTPHeaderField: \"Content-Type\")\n        request.httpMethod = \"POST\"\n        let parameters = [\"text\": text] as [String: Any]\n        let httpBodyData = try? JSONSerialization.data(withJSONObject: parameters, options: [])\n        request.httpBody = httpBodyData\n        wkWebView.load(request)\n\n        return wkWebView\n    }\n\n    func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext&lt;WebView>) {}\n\n    func makeCoordinator() -> WebView.Coordinator {\n        Coordinator(self)\n    }\n\n    class Coordinator: NSObject, WKNavigationDelegate {\n        var control: WebView\n\n        init(_ control: WebView) {\n            self.control = control\n        }\n\n        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {\n            NotificationCenter.default.post(name: .loadWebPageError, object: error.localizedDescription, userInfo: nil)\n        }\n\n        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {\n            if let url = webView.url?.absoluteString {\n                print(\"url = \\(url)\")\n                \/\/ \u83b7\u53d6\u52a0\u8f7d\u540e\u7684HTML\u7684url\uff0c\u5e76\u4e14\u53d1\u9001\u51fa\u53bb\u3002\n                NotificationCenter.default.post(name: .loadWebPageFinish, object: url, userInfo: nil)\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5f15\u7528WebViewPost\u52a0\u8f7d\u9875\u9762\nWebView(url: \"https:\/\/xxxxxxxxxxx\", text: \"test\").frame(width: 1, height: 1).zIndex(1)\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct XXXXXXXView: View {\n    \n\n    var loadWebPageFinish = NotificationCenter.default.publisher(for: .loadWebPageFinish)\n    var loadWebPageError = NotificationCenter.default.publisher(for: .loadWebPageError)\n\n    var body: some View {\n        VStack(spacing: 0) {\n            }\n        }.onReceive(loadWebPageFinish) { param in\n            \/\/ \u8fd9\u91cc\u9762\u83b7\u53d6\u5230webview\u7684url\uff0c \u7528\u6d4f\u89c8\u5668\u6253\u5f00\u3002\n            let url = URL(string: param.object as! String)\n            UIApplication.shared.open(url!, options: [:],\n                                      completionHandler: { _ in\n                                      })\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5728Swift\u4e2d\uff0c\u7528Safari\u6253\u5f00URL\u6bd4\u8f83\u7b80\u5355\u3002\u4f46\u662f\u8fd9\u90fd\u662fget\u8bf7\u6c42\u3002 \u5047\u5982\u6211\u4eec\u9700\u8981\u7528post\u8bf7\u6c42\u5148l [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4861"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4861"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4861\/revisions"}],"predecessor-version":[{"id":4866,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4861\/revisions\/4866"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}