0

I am developing one iOS application in which I want to load the HTML file on UIWebView, my html file path is as like below,

iPhone HTML file path:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

I want to load this iPhone device's HTML file path to UIWebView.

How can I do this?


Thank you very much for your quick answer. Let me explain my basic requirement, I am devleoping one iOS application in which my requirement is to fill some field on the screen and generate a .pdf file.

For that I have taken one pre-defined HTML form, and after that I updated that .html file and saved it via the code below:

NSString *fileName = @"new_file_htmlfile.html";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *html_file_name_location = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error;

// Write the file
[data writeToFile:html_file_name_location atomically:YES
        encoding:NSUTF8StringEncoding error:&error];

By doing that I have generated the new HTML file and I have read the file path from the code. The iPhone device path is:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

iPhone emulator path is:

/Users/rightwaysolution/Library/Application Support/iPhone Simulator/6.1/Applications/20748991-EE5C-44FE-ACFF-D93542FCF95B/Documents/new_file_htmlfile.html

Now I am trying to load that iPhone device HTML file on my UIWebView so that I can convert it into a .pdf.

Could you please explain to me how I can access that file?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
android.rws
  • 39
  • 1
  • 3
  • 9
  • The HTML file you want to read, needs to reside within you application sandbox. Refer above link to do that. It is not possible to read HTML file in some other application's bundle without jailbreak. – Amar Sep 10 '13 at 10:24

1 Answers1

6

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];
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
  • 1
    Hey, thanks for your answer but sorry to say as i have provided the path of html file at my question is not from the folder from xcode. as i have created one html file and i have saved that file on my local storage and after saving that html file i want that file to show on uiwebview. that path is surly from iphone internal storage or from iphone simulator internal storage. kindly help me if you give any answer to load html file from that iphone internal storage file. – android.rws Sep 10 '13 at 12:24
  • there are two ways you can read your local html file one is if your html file is inside your app and with xcode and other is your html file is at documents directory of your app...so if you need any help in these 2 case i can help you. – D-eptdeveloper Sep 10 '13 at 12:44
  • hey eptdeveloper , i have updated my question for your batter understanding of my requirement could you please help me ? – android.rws Sep 10 '13 at 13:46
  • from this way you can find your file stored at documents directory and then use it to open it on webview http://stackoverflow.com/a/6546616/2695503 – D-eptdeveloper Sep 11 '13 at 04:05
  • hey eptdeveloper, thank you very much for your answer , now i am read the file from that document dir path , now my new question is ,now i have read the html file which i have created , how i can load that file on UIWebView programatically ? could you please help me by what code i can load that file path ? my html file path is iphone path : /var/mobile/Applications/FEE7BD45-5714-4F26-A527-A1FBF2871644/Documents/filename.html – android.rws Sep 11 '13 at 07:18
  • @android.rws : try my edited answers with your html file hope it helps you. – D-eptdeveloper Sep 11 '13 at 07:23
  • I am very much thankful to you ! thank you so much for your ultimate support i am very much glad, by using your help code i can able to solve my issue. Thanks again. – android.rws Sep 11 '13 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37155/discussion-between-android-rws-and-eptdeveloper) – android.rws Sep 11 '13 at 09:26
  • thank yooooou, it works. – Bhimbim Jul 12 '17 at 11:31