2016-03-01 274 views
2

我想使用LoadFileUrl方法加載WKWebView中的本地html文件,但是我得到的只是一個空白視圖。這是一個Xamarin.Mac應用程序(尚未沙盒)。WKWebView使用LoadFileUrl加載本地內容

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)); 

使用「LoadRequest」從遠程服務器加載網頁時工作得很好。

爲「的Index.html」文件生成操作爲「BundleResource」

感謝您的幫助!

+0

http://stackoverflow.com/questions/24882834/wkwebview-not-loading-local-files-under-ios-8 – Gusman

+0

@Gusman感謝,但我已經看到了鏈接,如果我沒有弄錯,「LoadFileUrl」應該是解決方案,但它不起作用! 它在ElCapitan上可能還有錯誤嗎?我應該將html文件複製到「temp」文件夾中並從那裏加載它? – frenchfaso

+0

它應該加載,更好地看到webview檢查器,並查看加載時是否有任何錯誤:http://stackoverflow.com/questions/25200116/how-to-show-the-inspector-within-your-wkwebview-based-桌面應用程序 – Gusman

回答

2

克里斯哈蒙斯指出我在Xamarin forums正確的方向。 只是改變

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

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

的伎倆!

+1

您可以選擇您的答案作爲解決方案,然後:-) – testing

4

對於那些不使用Xamarin和與loadFileURL戰鬥:幾個小時(像我一樣)。

當您移動Web文件夾到項目中,選擇「創建文件夾引用」

enter image description here

然後使用代碼是這樣的:

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) 
    } 
} 

在HTML文件使用像這樣的文件路徑

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

不是這樣的

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

實例目錄 enter image description here

+0

謝謝!你的示例代碼和截圖幫助我調試了我正在追逐的一個問題。 – rams