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];
Related
This question is kind of self-explanatory but I'll explain it anyways. For some reason when I try and load a new web page it will see the web page URL except it won't load it the first time you press the button. Only when you press the button again will it actually load. How do I fix this?
Here's the code for my button:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
[_Hoot loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
*Also note, even though I'm copying the web page here and then loading it from a file, I still get the same problem if I'm just loading it from a URL.
Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
NSURL *urlen = [NSURL fileURLWithPath:filePath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[_Hoot loadRequest:urlRequest];
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
My app has to be able to download a file and display it. The format could be any of the major format types. I have been doing this with a UIWebView but when iOS 8 rolled out it broke this feature. I am wondering if this an issue with iOS 8 or is there a work around that I can use?
Here is my code:
//To show how I create the file path
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
//Name is the file's name that I get from the web service I use.
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:name];
- (void) showFile:(NSString *)path andFileName:(NSString *)name
{
dispatch_async(dispatch_get_main_queue(), ^{
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *targetURL = [NSURL fileURLWithPath:self.filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
self.webView.delegate = self;
[self.view addSubview:self.webView];
});
}
Thank you in advance.
I ran into the same problem rendering a pdf document on iOS 8.
So far, I've noticed this only for pdfs. So i check and see if the file is a pdf or not.
This is what i'm doing as a workaround.
NSData *pdfData = [[NSData alloc] initWithContentsOfFile:fileLocation];
[self.webView loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I found out what the issue was in my case and so I wanted to post the answer.
I was using a direct file path:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
When I should have been using a relative path like so:
NSString *resourceDocPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
resourceDocPath = [paths objectAtIndex:0];
They changes the file structure for iOS 8 which is why my code wasn't working since I was looking in the wrong place. I hope this helps someone else.
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
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