{"id":591,"date":"2023-02-25T19:57:54","date_gmt":"2023-02-25T11:57:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=591"},"modified":"2023-04-29T20:29:44","modified_gmt":"2023-04-29T12:29:44","slug":"swift-ui-web-page-control-uiwebview-load-local-data-file","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-web-page-control-uiwebview-load-local-data-file\/","title":{"rendered":"Swift UI &#8211; \u7f51\u9875\u63a7\u4ef6\uff08UIWebView\uff09\u52a0\u8f7d\u672c\u5730\u6570\u636e\uff0c\u6587\u4ef6"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p>\u4f7f\u7528UIWebView\u52a0\u8f7d\u672c\u5730\u6570\u636e\u6216\u8d44\u6e90\u6709\u5982\u4e0b\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>loadHTMLString<\/code>\u65b9\u6cd5\u52a0\u8f7dHTML\u5185\u5bb9<\/li>\n<li>\u4f7f\u7528<code>loadRequest<\/code>\u65b9\u6cd5\u52a0\u8f7d\u672c\u5730\u8d44\u6e90\uff08\u4e5f\u53ef\u7528\u4e8e\u52a0\u8f7d\u670d\u52a1\u5668\u8d44\u6e90\uff09<\/li>\n<li>\u5148\u5c06\u5185\u5bb9\u4fdd\u5b58\u6210<code>Data<\/code>\u6570\u636e\uff0c\u518d\u4f7f\u7528<code>load<\/code>\u65b9\u6cd5\u52a0\u8f7d<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>\u4f7f\u7528\u6837\u4f8b\uff1a\u901a\u8fc7 <code>UISegmentedControl<\/code>\u5206\u522b\u63a7\u5236\u7f51\u9875\u63a7\u4ef6\u201c\u663e\u793aHTML\u201d\u3001\u201c\u663e\u793aPDF\u201d\u3001\u201cloadData\u663e\u793aPDF\u201d<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController {\n\n    @IBOutlet var webView:UIWebView!\n    @IBOutlet var loadTypeSegment:UISegmentedControl!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        \/\/\u9ed8\u8ba4\u9009\u4e2d\u5206\u6bb5\u63a7\u4ef6\u7684\u7b2c\u4e00\u9879\n        loadTypeSegment.selectedSegmentIndex = 0\n        typeChanged(loadTypeSegment)\n    }\n\n    @IBAction func typeChanged(_ sender:UISegmentedControl) {\n        let index = sender.selectedSegmentIndex\n        print(index)\n        switch index {\n        case 0: \/\/\u5728 UIWebView \u4e2d\u663e\u793a HTML \u5185\u5bb9\n            let html = &quot;&lt;h1&gt;\u6b22\u8fce\u6765\u5230\uff1a&lt;a href=&#039;http:\/\/hangge.com&#039;&gt;\u822a\u6b4c&lt;\/a&gt;&lt;\/h1&gt;&quot;;\n            webView.loadHTMLString(html, baseURL:nil)\n        case 1: \/\/\u5728 UIWebView \u4e2d\u663e\u793a PDF\n            let path = Bundle.main.path(forResource: &quot;test&quot;, ofType:&quot;pdf&quot;)\n            let urlStr = URL(fileURLWithPath: path!)\n            print(urlStr)\n            webView.loadRequest(URLRequest(url:urlStr))\n        case 2: \/\/\u5728 UIWebView \u4e2d\u663e\u793a PDF, \u4f46\u662f\u662f\u901a\u8fc7 loadData \u65b9\u5f0f\u52a0\u8f7d\n            let path = Bundle.main.path(forResource: &quot;test&quot;, ofType:&quot;pdf&quot;)\n            let urlStr = URL(fileURLWithPath: path!)\n            let data = try! Data(contentsOf: urlStr)\n            webView.load(data, mimeType: &quot;application\/pdf&quot;, textEncodingName: &quot;utf-8&quot;,\n                         baseURL: NSURL() as URL)\n        default:\n            print(&quot;Segmente Error&quot;)\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u4f7f\u7528UIWebView\u52a0\u8f7d\u672c\u5730\u6570\u636e\u6216\u8d44\u6e90\u6709\u5982\u4e0b\u4e09\u79cd\u65b9\u5f0f\uff1a \u4f7f\u7528loadHTMLS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}