Load resources from relative path using local html in uiwebview
This is how to load/use a local html with relative references.
- Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders".
- Select the "create folder references.." option.
Use the below given code. It should work like a charm.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
Now all your relative links(like img/.gif, js/.js) in the html should get resolved.
Swift 3
if let path = Bundle.main.path(forResource: "dados", ofType: "html", inDirectory: "root") {
webView.load( URLRequest(url: URL(fileURLWithPath: path)) )
}
iOS UIWebview Load local device HTML file path
use this code to achieve what you want
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"SarkBuilerHtmlFile_new" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
but for this make sure your html file must be within your app folder in xcode
Edits:-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
OR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
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
Loading a UIWebview with a local HTML file from a nested folder structure
oops, I actually found the answer here: Load resources from relative path using local html in uiwebview
I was able to use the actual folder structure as is with the following code:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/about/page"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
HTML Link from within a local HTML file in a UIWebView
I suggest you re-examine your directory hierarchy. The behavior you are describing is what happens when the link you are trying to navigate to does not exist on the local device.
I ran the following small test:
Added a Webview to a view controller and loaded page1.html
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 10, 320, 300)];
[self.view addSubview:webView];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"page1" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];
page1.html
<html>
<body>
<h1>Page 1</h1>
<a href="page2.html">Go to Page2</a>
</body>
</html>
page2.html
<html>
<body>
<h1>Page 2</h1>
<a href="page1.html">Back to Page 1</a>
</body>
</html>
Image of the project Structure
The end result is, that it works. Clicking takes you from page 1 to page 2 and back again all using local files. If we change the link to:
<a href="page3.html"></a>
which does not exist, you get the same behavior you are experiencing. That is you can only cut and copy and not actually go to the link.
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]]];
Related Topics
How to Create Delegates in Objective-C
How to Develop or Migrate Apps For Iphone 5 Screen Resolution
How to Develop For Iphone Using a Windows Development Machine
How to Find Topmost View Controller on Ios
How to Adjust the Anchor Point of a Calayer, When Auto Layout Is Being Used
How to Take a Screen Shot of a Uiview
How to Debug 'Unrecognized Selector Sent to Instance' Error
Undefined Symbols For Architecture Armv7
The Simplest Way to Resize an Uiimage
Check For Internet Connection With Swift
Nsnotificationcenter Addobserver in Swift
Converting String to Int With Swift
Attempt to Present Uiviewcontroller on Uiviewcontroller Whose View Is Not in the Window Hierarchy
Detecting Which Uibutton Was Pressed in a Uitableview