2016-09-27 111 views
0

試圖在Xcode中創建第一個應用程序:將我的Web應用程序文件夾並嘗試使用WebView將其實現到XCode項目。通過File在模擬器總是黑屏......Webview的本地文件路徑

我開始新的項目,創建WebView並實施我的Web文件夾應用程序 - - 但有沒有成功>Add files to "MyProject"與選項設置爲複製文件和設置網址到本地index.html視圖控制器。我究竟做錯了什麼?

項目結構:

-dist 
--index.html 
--scriptsbundle.js 
--img 
---myimages.png 

-MyProject 

-MyProject.xcodeproj 

-MyProjectTests 

-MyProjectUITests 

ViewController.swift

import UIKit 

class ViewController: UIViewController { 


    @IBOutlet var Webview: UIWebView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let url = NSURL(string: Bundle.path(forResource: "index", ofType: "html", inDirectory:"../dist")!) 
     let request = NSURLRequest(url: url as! URL) 

     Webview.loadRequest(request as URLRequest) 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 


} 
+1

你有沒有嘗試過這裏提供的解決方案? http://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift –

+0

好的,謝謝。但經過Swift版本的一些重構原因思想。現在所有的作品,除了'img'文件夾中的所有圖像(只有一個svg圖像,以css風格實現):( –

回答

0

正如評論,這阿莫德戈卡萊寫道,另一篇文章中救了我的命,但有點重構後:

let path = Bundle.main.path(forResource: "index", ofType:"html") 
let url = NSURL(string: path!) 
let request = NSURLRequest(url: url as! URL)