1

I have an html file and other files that html uses(css,.png) that is saved in the documents directory.How can I load this html file in a UIWebView or wkwebview using swift?I have found some examples in objective-c but nothing in swift.I don't know anything about objective-c..

let path=getCurrenttHtmlStartPage()
var hContent = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) 
webView!.loadHTMLString(hContent, baseURL: nil)
webView!.hidden=false 

Path is the path in documents folder. /Users/pmdevios/Library/.../Documents/Content/Html/index.html.

With this way the other files inside html aren't showing(images) so I want to do it with another way like this

webView!.loadFileURL(path, allowingReadAccessToURL: path)
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
solero_ice
  • 49
  • 1
  • 6
  • can you please post your code here.. – Nimit Parekh Dec 15 '15 at 14:25
  • duplicate http://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift – Igor Dec 15 '15 at 14:28
  • @NimitParekh So far I have only this: 'let path=getCurrenttHtmlStartPage() var hContent = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) webView!.loadHTMLString(hContent, baseURL: nil) webView!.hidden=false ' . path is the path in documents folder (/Users/pmdevios/Library/.../Documents/Content/Html/index.html) .With this way the other files inside html aren't showing(images) so I want to do it with another way like this 'webView!.loadFileURL(path, allowingReadAccessToURL: path)' or wit a NSUrlRequest but i cannot make it work. – solero_ice Dec 15 '15 at 14:37
  • @Igor it is not duplicate what I'm asking is load from Documents directory a file that is download in runtime and not from application resources! – solero_ice Dec 15 '15 at 14:39
  • @Nimit Parekh it's the same only difference - how to build the path, and this question is very well documented by apple – Igor Dec 15 '15 at 14:44
  • @solero_ice have use image into html file? – Nimit Parekh Dec 15 '15 at 14:47
  • @NimitParekh yes.Also i have found this http://stackoverflow.com/questions/12984502/display-local-html-file-from-documents-directory-in-a-uiwebview-on-iphone that might be the answer but is in objective-c and i have trouble with this. – solero_ice Dec 15 '15 at 14:51
  • @solero_ice Can you post your html code here. – Nimit Parekh Dec 15 '15 at 14:51
  • @NimitParekh it is something simple.here http://pastebin.com/Pg0jVkZe – solero_ice Dec 15 '15 at 14:57
  • @solero_ice I need to debug your code if you have code can you please send it to me. – Nimit Parekh Dec 15 '15 at 15:01
  • @NimitParekh thanks but i make it work.Thank you for your comments! – solero_ice Dec 15 '15 at 15:17

3 Answers3

2

With the below code it worked!

let filePath = (folder as  NSString).stringByAppendingPathComponent(_currentHtmlStartPage!)
var url:NSURL=NSURL(fileURLWithPath:filePath)
var request:NSURLRequest=NSURLRequest(URL:url)
webView!.loadRequest(request)

folder:string that represents the path in the documents folder _currentHtmlStartPage:string of file's name (e.g. index.html)

solero_ice
  • 49
  • 1
  • 6
1

EDIT

  1. Get URL using NSFileManger instance method URLsForDirectory(inDomains:)

  2. Append the file name (myFile.html) to the URL we get back from NSFileManager

  3. Initialize a NSURLRequest object with the URL.

  4. Use the UIWebView or WKWebView class to load the request.


let fileManager = NSFileManager.defaultManager()
var URL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
URL = URL.URLByAppendingPathComponent("myFile.html")

let request = NSURLRequest(URL: fileURL)
webView.loadRequest(request)
Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – NathanOliver Dec 15 '15 at 17:26
  • 1
    @NathanOliver, I have edited my answer to include an explanation of the code. Appreciate the link and feedback. – Peter Hornsby Dec 15 '15 at 17:40
0

Of the top of my head:

if let fileURL = NSBundle.mainBundle().URLForResource("myFile", withExtension: "html") {
    let request = NSURLRequest(URL: fileURL)
    webView.loadRequest(request)
}

You'll need a flat directory structure in your html and css files because that's how they end up in the app.

Adis
  • 4,512
  • 2
  • 33
  • 40