Open downloaded files in webview in iphone - iphone

I am new to iphone development. I am developing an iphone application in which i am downloading files using web service with following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSData *fileData = [self decodeBase64WithString:soapResults];
[fileData writeToFile:fileLoc atomically:YES];
And my file is in path : /Users/myusername/Library/Application Support/iPhone Simulator/4.0/Applications/7A627E64-DEAB-40FA-BE04-35ED50580B65/filename.pdf
How can i open this file using uiwebview??? I tried this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename"];
[self loadDocument:fileLoc inView:mywebview];
My loadDocument method is:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
But it showing error... I think location is the problem.. How can i find the path in simulator as well as in orignal device???
Please help me...

pathForResource:ofType: can only be used to load resource files, which are shipped as part of your application bundle. You're trying to load a downloaded file from your Documents directory. So you should be able to just do
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

What is the error you are getting? What does your loadDocument method look like? You should load the document into the WebView something like this:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileLoc]]];

Related

Display Local HTML file from documents directory in a UIWebView on iPhone

How can I display a HTML file from the Documents directory in my app?
Below is the code that I'm using and nothing is showing up in my UIWebView
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Notes.html"];
NSURL *url = [NSURL URLWithString:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[notesListWeb loadRequest:request];
Why is this not working out?
I've found other answers but they aren't from the documents directory.
Your help is greatly appreciated.
The proper way to create an NSURL object with a file path is:
NSURL *url = [NSURL fileURLWithPath:filePath];
You should also change how you create your filePath:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Notes.html"];
Also keep in mind that filenames are case sensitive on a real iOS device. Is the file really named Notes.html and notes.html?

How to read pdf file from document directory in iPhone?

Currently i am working in iPhone application, i have an pdf file in resource folder (Local pdf file) then i read that pdf file (paper.pdf) successfully, below i have mentioned read local pdf file for your reference.
Example:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
Then i have tried to store pdf file (from URL) in NSDocument directory, stored successfully.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.msy.com.au/Parts/PARTS.pdf"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
[pdfData writeToFile:filePath atomically:YES];
Then i tried read that pdf file (from NSDocument directory) but i didn't know that, please help me.
Thanks in Advance
its very simple please use below code to display your pdf from document directory to Webview
load PDF from Document Directory
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
}
load PDF from Application Bundle Resource Folder
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPDF11" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
}
Try to use this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
From NSURL reference:
The NSURL class is toll-free bridged with its Core Foundation counterpart, CFURLRef. For more information on toll-free bridging, see “Toll-Free Bridging”.

CGPDFDocumentCreateWithURL will download the file in to documents directory or not

I am accessing the PDF files available in my PHP server using my iPhone with the help of passing file url to CGPDFDocumentCreateWithURL. My question whenever i access the files it will downloaded to my iphone documents directory or not?
Because if my application in active for some time i am getting the error CFURLCreateDataAndPropertiesFromResource with error code -14. After getting this error my application is not crashed but it is not functioning properly.
I am not able to understand the internal process? Please help me in this issue?
I think you're misunderstanding what CGPDFDocumentCreateWithURL does. It does not download a PDF document to the filesystem. It creates a PDF in memory based off the URL provided. It doesn't save anything to the filesystem.
I would strongly recommend you use a networking class (NSURLConnection, ASI, etc) to download the PDF first and then open it. This will enable you to provide better feedback, and support more advanced download features such as resuming partial downloads, providing progress, and better thread management.
To save the PDF
NSString *urlString = #"http://www.web.com/url/for/document.pdf";
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"myLocalFileName.pdf"];
[data writeToFile:pdfPath atomically:YES];
then load that saved file into a web view:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"myLocalFileName.pdf"];
NSURL *url = [NSURL fileURLWithPath: pdfPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[self mainWebView] loadRequest:request];

iOS download and save HTML file

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
This is what I have tried -
NSString *stringURL = #"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
Not too sure what I have done wrong?
Any help would be appreciated. Thanks!
The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.
NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
To give you'll want to change this line.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
Here is the Swift 2.x version:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))

How to create iPhone application can download content

Now I try to build iPhone application, like magazine online, that can download new magazine form web and store in app (can play offline).
I no idea for implement feature download new magazine form online source and store in the app.
Please help me.
Here's an example how you can download a file to your iPhone and reuse it later (in this case it's stores an HTML document that is then loaded to UIWebView from local store).
// downloading file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *dataPath = [paths objectAtIndex:0];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [dataPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
NSString *fileString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
[fileString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
// use local file
NSURL *loadUrl = [[NSURL alloc] initFileURLWithPath: filePath];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: loadUrl];
[webView loadRequest: request];
[request release];
[loadUrl release];