I would like to load html files, which are downloaded into document folder. First page is loaded ok, but links to second page not works and images not work too. I am using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:#"index.htm"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[pruvodceWebView loadHTMLString:htmlString baseURL:nil];
Is there any reason, why webview do this? Thank you
The problem is that you have HTML source but not images
Instead of
[pruvodceWebView loadHTMLString:htmlString baseURL:nil];
You should define baseURL. If your images are in documents directory
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[pruvodceWebView loadHTMLString:htmlString baseURL:baseURL];
Or provide path to images if they have relative path
[pruvodceWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString#"http://yourSiteHere.com/"]];
Check out this:
Load resources from relative path using local html in uiwebview
Related
Have a Binary code convert it to nsdata using dataFromBase64String. And load it web view successfully. But Want the pdf file do not store in documentsDirectory.Need to add one button click user click the button, then Binary to pdf convert will happened and display in web view. here my code. How is possible help me. thanks advance.
NSString *binaryString =#"Binary code to change";
myData = [NSData dataFromBase64String: binaryString];
[self savePDF:myData];
- (void)savePDF:(NSData *)pdfContent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
finalPath = [documentsDirectory stringByAppendingPathComponent:#"myPdf.pdf"];
NSLog(#"%#",finalPath);
NSURL *url = [NSURL fileURLWithPath:finalPath];
[pdfContent writeToURL:url atomically:YES];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
If you don't want to store pdf file to DocumentsDirectory then
try following, hope it will work for you
NSString *binaryString =#"Binary code to change";
myData = [NSData dataFromBase64String: binaryString];
[self savePDF:myData];
- (void)savePDF:(NSData *)pdfContent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
finalPath = [documentsDirectory stringByAppendingPathComponent:#"myPdf.pdf"];
NSLog(#"%#",finalPath);
NSURL *url = [NSURL fileURLWithPath:finalPath];
//[pdfContent writeToURL:url atomically:YES];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Comment //[pdfContent writeToURL:url atomically:YES]; line
and check
Is there an ability to load files like documents, articles from server and store them in application local storage using Filemanager? And how then users can read these files? Do they have access to that folder?
Follow these steps for downloading and saving file in NSDocumentDirectory
// Determine local file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"fileName.doc"]; //it could be any file type
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.filePathFromServerLocation.com"];
//and write to file
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
This way your file will be save at filePath and u can use it in future whatever the way u like.
Opening a file in iOS:
Using a webview
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
using a UIDocumentInteractionController.
Have a look at this Apple sample code for ZoomingPDFViewer
I am working with UIWebview. I am able to load UIWebView with html file and because of some need in my project i want to read complete data from UIWebView. and I want to display in another UIWebView. but my need is I want to save the html data in one local folder what I read alredy. and this save data I want to load in second webview.this is code to load webview and read webview content.How to save in localfolder and retrive from that folder. Can any one help me
get local html file load into the webview:
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"b_PctureCardHelp" ofType:#"html"]]]];
Read webview Content
NSString *yourHTMLSourceCodeString = [webView1 stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];
[webView2 loadHTMLString:yourHTMLSourceCodeString baseURL:nil];
To Save HTML in local documents folder use this
- (void)SaveInfo :(NSString *)HTMLString
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"htmlName.html"];
NSString *helloStr = HTMLString;
NSError *error;
[helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
For getting the path and loading
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"htmlName.html"];
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
Save Your Html Stirng File to app/Library/Cache
NSString *str_html = #"html string";
//yourHTMLSourceCodeString String
NSString *str_lib_path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Get Library Path
NSString *str_cache_path = [str_lib_path stringByAppendingPathComponent:#"Caches"];
//Get Caches Path
NSString *str_file_path = [str_cache_path stringByAppendingPathComponent:#"file.html"];
//yourHTMLSourceCodeString File Path
[str_html writeToFile:str_file_path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//Save your html string to file path
//then you should find the file in /var/mobile/Applications/(your app uuid)/Library/Caches/file.html
Load Html String File
NSString *str_lib_path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *str_cache_path = [str_lib_path stringByAppendingPathComponent:#"Caches"];
NSString *str_file_path = [str_cache_path stringByAppendingPathComponent:#"file.html"];
//your html string file path
NSString *str_html = [NSString stringWithContentsOfFile:str_file_path encoding:NSUTF8StringEncoding error:nil]
//Get your html file from local file
Check File Path
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:str_cache_path]) //check caches folder is exist or not
{
[fm createDirectoryAtPath:str_cache_path withIntermediateDirectories:NO attributes:nil error:nil]
//create caches folder
}
//you can also use this method to check the html file is exist or not
wish help ~
I tried to show an html page that is inside my app, but it did not show on the view:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"abt.html"];
NSURLRequest *documentsRequest = [NSURLRequest requestWithURL:
[NSURL fileURLWithPath:path]] ;
[_webview loadRequest:documentsRequest] ;
I followed this link: iOS - display content from an html resource file or remote webpage in a webview
by Matt Gibson
Please let me know how to show.
thanks
As you said your html page is inside the app so try this one, I have used it and it's work fine.
NSString *htmlFile=[[NSBundle mainBundle] pathForResource:#"abt" ofType:#"html"];
NSString *htmlString=[NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:Nil];
[_webview loadHTMLString:htmlString baseURL:nil];
I am making a pdf using UIGraphicsBeginPDFPageWithInfo from images,then loading that pdf into a UIWebView.
I want to select some text from that pdf and show it on a textview. Here is the code sample I am using to load a pdf on a web view.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"images.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
NSLog(#"pdf URL :: %#",pdfUrl);
NSURLRequest *request = [NSURLRequest requestWithURL:pdfUrl];
[_webView loadRequest:request];
I have tried using the following, but it is returning a blank string.
NSString *innerText = [_webView stringByEvaluatingJavaScriptFromString:#"document.body.innerText"];
edit:: I want the text selection functionality as implemented in ibooks.
can you see this example in gitHub: https://github.com/KurtCode/PDFKitten