3

I'm trying to load a local html file in a WKWebView with the LoadFileUrl method, but all I get is a blank view. It's a Xamarin.Mac App (no Sandbox yet).

WKWebViewConfiguration conf = new WKWebViewConfiguration();
WKWebView www = new WKWebView (View.Frame, conf);
View = www;

string index = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp/Index.html");
string webAppFolder = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp");

www.LoadFileUrl (new NSUrl ("file://"+index), new NSUrl ("file://"+webAppFolder));

Loading a webpage from a remote server with "LoadRequest" works just fine.

Build Action for the "Index.html" file is "BundleResource"

Thanks for all your help!

frenchfaso
  • 481
  • 5
  • 17
  • http://stackoverflow.com/questions/24882834/wkwebview-not-loading-local-files-under-ios-8 – Gusman Mar 01 '16 at 14:59
  • @Gusman thanks, but I already saw the link, if I got it right, "LoadFileUrl", should be the solution, but it doesn't work! Is it perhaps still buggy on ElCapitan? Should I copy the html file in the "temp" folder and load it from there? – frenchfaso Mar 01 '16 at 15:48
  • It should load, better see the webview inspector and see if there is any error when loading: http://stackoverflow.com/questions/25200116/how-to-show-the-inspector-within-your-wkwebview-based-desktop-app – Gusman Mar 01 '16 at 15:50

2 Answers2

5

For those not using Xamarin and fighting with loadFileURL: for hours(like me).

When you are moving the web folder to a project, select "Create folder references"

enter image description here

Then use code that is something like this:

if let filePath = NSBundle.mainBundle().resourcePath?.stringByAppendingString("/WebApp/index.html"){
  let url = NSURL(fileURLWithPath: filePath)
  if let webAppPath = NSBundle.mainBundle().resourcePath?.stringByAppendingString("/WebApp") {
    let webAppUrl = NSURL(fileURLWithPath: webAppPath, isDirectory: true)
    webView.loadFileURL(url, allowingReadAccessToURL: webAppUrl)
  }
}

In the html file use filepath like this

<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

not like this

<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">

Example directory enter image description here

Koodimetsa
  • 760
  • 9
  • 11
  • THANK YOU! Your sample code and screenshots helped me debug an issue I was chasing. – rams Aug 24 '16 at 15:11
3

Chris Hamons pointed me in the right direction on the Xamarin forums. Just changing

string index = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp/Index.html");
string webAppFolder = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp");

to

string index = Path.Combine (NSBundle.MainBundle.ResourcePath, "WebApp/Index.html");
string webAppFolder = Path.Combine (NSBundle.MainBundle.ResourcePath, "WebApp");

did the trick!

frenchfaso
  • 481
  • 5
  • 17