使用swift将本地html加载到UIWebView中

这个今天早上让我疯了。 我想加载一些本地的HTML到网页视图:

class PrivacyController: UIViewController { @IBOutlet weak var webView:UIWebView! override func viewDidLoad() { let url = NSURL(fileURLWithPath: "privacy.html") let request = NSURLRequest(URL: url!) webView.loadRequest(request) } } 

该html文件位于我的项目的根文件夹,但在一个组内。 webview对我来说是空白的。 任何想法什么是错的? 我在Xcode 6.1上,并在我的iPhone 6上运行这个例子。

要检索应用程序资源的URL,应使用NSBundle类的URLForResource方法。

Swift 2

 let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html") 

Swift 3

 let url = Bundle.main.url(forResource: "privacy", withExtension: "html") 
 // Point UIWebView @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() //load a file var testHTML = NSBundle.mainBundle().pathForResource("privacy", ofType: "html") var contents = NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil) var baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file webView.loadHTMLString(contents, baseURL: baseUrl) } 

Swift 3:types安全

 @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Adding webView content do { guard let filePath = Bundle.main.path(forResource: "myFile", ofType: "html") else { // File Error print ("File reading error") return } let contents = try String(contentsOfFile: filePath, encoding: .utf8) let baseUrl = URL(fileURLWithPath: filePath) webView.loadHTMLString(contents as String, baseURL: baseUrl) } catch { print ("File HTML error") } } 

请记住:NS =不是Swift:]

将本地html文件添加到您的项目名称中作为home.html,然后使用NSURL对象创buildNSURLRequest,然后将请求传递给webview,它会将请求的URL加载到webview中,如果您没有使用storyboard,则将uiwebview添加到viewcontroller视图中像下面的代码。

  override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.    let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html");    let myRequest = NSURLRequest(URL: localfilePath!);    myWebView.loadRequest(myRequest); self.view.addSubview(myWebView)  } 

有关更多参考资料,请参阅http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/

Swift版本2.1

这种情况还包括编码

 // load HTML String with Encoding let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html") do { let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding) webView.loadHTMLString(fileHtml as String, baseURL: nil) } catch { } 

这对我有用:

 @IBOutlet weak var mWebView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. mWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("fineName", ofType: "html")!))) } 

info.plist文件中添加了Dictionarytypes的App Transport Security Settings 。 还添加了子键Allow Arbitrary Loads 应用程序传输安全设置的 Allow Arbitrary Loads ,types为Boolean ,值为YES

这里是教程。

EDITED

对于Swift 3(Xcode 8)

 mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!))) 

斯威夫特33🙂

 if let url = Bundle.main.url(forResource: "privacy", withExtension: "html") { webview.loadRequest(URLRequest(url: url)) } 

您可以在UIWebView中加载htmlstring或本地html文件。

HTMLstring:

 func loadHtmlCode() { let htmlCode = "<html><head><title>Wonderful web</title></head> <body><p>wonderful web. loading html code in <strong>UIWebView</strong></></body>" webView.loadHTMLString(htmlCode, baseURL: nil) } 

HTML文件:

 func loadHtmlFile() { let url = NSBundle.mainBundle().URLForResource("contactus", withExtension:"html") let request = NSURLRequest(URL: url!) webView.loadRequest(request) } 

详情可以在这里find: http : //webindream.com/load-html-uiwebview-using-swift/

这工作对我来说(Xcode 8,Swift 3)

@IBOutlet弱var webViewer:UIWebView!

 override func viewDidLoad() { super.viewDidLoad() let localfilePath = Bundle.main.url(forResource: "homeInfo", withExtension: "html"); let myRequest = NSURLRequest(url: localfilePath!); webViewer.loadRequest(myRequest as URLRequest); self.view.addSubview(webViewer) } 

对于swift 3使用这个:

 do { let testHTML = Bundle.main.path(forResource: "about", ofType: "html") let contents = try NSString(contentsOfFile: testHTML!, encoding: String.Encoding.utf8.rawValue) let baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file mWebView.loadHTMLString(contents as String, baseURL: baseUrl as URL) } catch { }