Load Local HTML into Uiwebview Using Swift

Load local html into UIWebView using swift

To retrieve URLs for application resources, you should use URLForResource method of NSBundle class.

Swift 2

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

Swift 3

let url = Bundle.main.url(forResource: "privacy", withExtension: "html")

How to load local html file into UIWebView with swift4.2

In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.

  import WebKit

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let localFilePath = Bundle.main.url(forResource: "document_terms_of_use", withExtension: "html")
let request = URLRequest(url: localFilePath!)
webView.load(request as URLRequest)
}

Load local html into UIWebView using swift

To retrieve URLs for application resources, you should use URLForResource method of NSBundle class.

Swift 2

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

Swift 3

let url = Bundle.main.url(forResource: "privacy", withExtension: "html")

How can I load a local HTML file into the UIWebView?

The following code will load an HTML file named index.html in your project folder:

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

How to load local html file into UIWebView

probably it is better to use NSString and load html document as follows:

Objective-C

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];

Swift

let htmlFile = NSBundle.mainBundle().pathForResource("fileName", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(html!, baseURL: nil)

Swift 3 has few changes:

let htmlFile = Bundle.main.path(forResource: "intro", ofType: "html")
let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
webView.loadHTMLString(html!, baseURL: nil)

Did you try?

Also check that the resource was found by pathForResource:ofType:inDirectory call.

Edit and Load HTML from local file in Swift

Found the solution :
If you are applying some local css whose path is there in HTML file.
viz.

  1. Add that css file "myDefault.css" to your xcode project directory.
  2. Convert the HTML to string and replace the path of css in HTML file with your local path. :
  3. Load the content of HTML to UIWebView with base url. (Providing local file path as baseUrl is important)

    webView = UIWebView()

    let pathToInvoiceHTMLTemplate = Bundle.main.path(forResource: "presc", ofType: "html")
    let pathToHTMLCSS = Bundle.main.path(forResource: "myDefault", ofType: "css")
    do {
    var strHTMLContent = try String(contentsOfFile: pathToInvoiceHTMLTemplate!)

    strHTMLContent = strHTMLContent.replacingOccurrences(of: "./assets/stylesheets/myDefault.css", with: pathToHTMLCSS!)

    let pdfURL = URL(fileURLWithPath: pathToInvoiceHTMLTemplate!)
    webView.loadHTMLString(strHTMLContent, baseURL: pdfURL)

    }catch let err{
    print(err)
    }

Display local HTML page in UIWebView

your problem is that on the app's bundle, there are no folders image or css, you just copied the files, without the folder structure

Try this:

  1. Click on HTML Files group and press delete, select Remove
    references

  2. Go to the Finder folder HTML Files and drag the entire folder to

    the project, select Create folde references

  3. A blue folder should appear on your project

  4. If you check your target under Build Phases>Copy Bundle Resources, you'll see that the entire folder structure is now added to your bundle

Load local HTML file in WebView with Swift 3 in a macOS app

Try this simple steps, for me works

let urlpath = Bundle.main.path(forResource: "index", ofType: "html");
let requesturl = URL(string: urlpath!)
let request = URLRequest(url: requesturl!)
webView.mainFrame.load(request)


Related Topics



Leave a reply



Submit