Previously, when loading local html content into UIWebView, it would automatically run localhost/server in the background. This server emulation would enable me to load dynamic content through json, for example. Example code below;
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "www/index", ofType: "html")!)))
}
I am now trying to implement this into a WKWebView. I can load local html content, but unlike UIWebView, WKWebView does not emulate localhost/server, so I cannot do things like before, such as dynamically load content with json etc. How would I go about running local html content through localhost? If UIWebView had that feature automatically, surely WKWebView should have it right? Code below.
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.load(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "www/index", ofType: "html")!)))
}
Note: I am using Xcode 9 for this, so WKWebview is being added through Storyboard and the referenced as an outlet.
Many thanks in advance to anybody who can help me with this.
Add below to your info.plist and start the localhost server
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With