为WKWebkitView运行localhost

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为WKWebkitView运行localhost相关的知识,希望对你有一定的参考价值。

以前,在将本地html内容加载到UIWebView时,它会在后台自动运行localhost / server。例如,这种服务器仿真使我能够通过json加载动态内容。示例代码如下;

@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "www/index", ofType: "html")!)))
}

我现在正在尝试将其实现为WKWebView。我可以加载本地html内容,但与UIWebView不同,WKWebView不会模拟localhost / server,所以我不能像以前那样做,比如用json等动态加载内容。我如何通过localhost运行本地html内容?如果UIWebView自动拥有该功能,那么WKWebView肯定应该拥有它吗?代码如下。

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.load(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "www/index", ofType: "html")!)))
}

注意:我正在使用Xcode 9,因此WKWebview通过Storyboard添加,并作为插座引用。

非常感谢任何可以帮助我的人。

答案

启动localhost服务器并将其添加到info.plist

<key>NSAppTransportSecurity</key>  
<dict>  
    <key>NSExceptionDomains</key>  
    <dict>  
        <key>127.0.0.1</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
        <key>localhost</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
    </dict>  
</dict> 

以上是关于为WKWebkitView运行localhost的主要内容,如果未能解决你的问题,请参考以下文章

Docker在本地打包maven程序为docker镜像报错: Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:(代

Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/项目名]](代

报错集:给新创建的MySQL数据库设置用户密码时报错(error: ‘Access denied for user ‘root‘@‘localhost‘ (using password: NO))(代

解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes(示例代

Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use.(示例代

解决 Streaming server stopped unexpectedly: listen tcp: lookup localhost on 114.114.114.114:53: no s(代