我们可以在本机iOS应用程序中创建localhost

时间:2017-11-17 06:12:54

标签: ios swift uiwebview

我必须在UIWebView或WKWebView中使用http协议加载html文件。这是我使用WKWebView的代码:

import UIKit
import WebKit

class ViewController: UIViewController,WKUIDelegate {

    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: self.view.frame, configuration: webConfiguration)
        webView.uiDelegate = self

        let path = URL(fileURLWithPath: "index.html")
        let myURL = path
        let myRequest = URLRequest(url: myURL)
        webView.load(myRequest)
        self.view.addSubview(webView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

是否可以从原生包中解析http protocal?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。您需要在应用程序中运行HTTP服务器才能实现此目的。就个人而言,我建议使用GCDWebServer