use*_*152 3 xcode uiwebview ios swift wkwebview
以前,将本地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")!)))
}
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试将其实现到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")!)))
}
Run Code Online (Sandbox Code Playgroud)
注意:我正在为此使用Xcode 9,因此通过Storyboard添加了WKWebview并将其引用为插座。
在此先感谢任何可以帮助我的人。
将以下内容添加到您的info.plist中,并启动localhost服务器
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>127.0.0.1:80</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>localhost:port</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)