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.
- Add that css file "myDefault.css" to your xcode project directory.
- Convert the HTML to string and replace the path of css in HTML file with your local path. :
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:
Click on
HTML Files
group and press delete, selectRemove
referencesGo to the Finder folder
HTML Files
and drag the entire folder to
the project, selectCreate folde references
A blue folder should appear on your project
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
Uiscreen Mainscreen Bounds Returning Wrong Size
Using Scrollview Programmatically in Swift 3
Creating Custom Info Window in Swift with the Google Maps iOS Sdk
How to Password Protect Writing to Nfc Ntag216 Tag on iOS 13 Using Nfc Core
iPhone App Under Test Crashes After a Few Days
Monotouch/Wcf: How to Consume the Wcf Service Without Svcutil
Making a Button Persistent Across All View Controllers
How to Programmatically Enable Guided Access (Kiosk Mode) on an Iphone
Optional Binding with Try? and As? Still Produces an Optional Type
Open Phone Settings Programmatically in iOS9
Performance Testing in Swift Using Tdd
How to Detect Swipe Gesture in iOS
Custom Alert (Uialertview) with Swift
Swift - Which Types to Use? Nsstring or String
@Binding and Foreach in Swiftui