Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running localhost for WKWebkitView

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.

like image 659
user1391152 Avatar asked Dec 19 '22 04:12

user1391152


1 Answers

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> 
like image 66
Cyber Avatar answered Jan 30 '23 20:01

Cyber