i want to download pdf file from webservice and open it with UIDocumentInteractionController.how can i do it? i use following code for download
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"myurl/abc.pdf"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"abcd.pdf"];
pdfData writeToFile:filePath atomically:YES];
now what should i do to open it with UIDocumentInteractionController
you can open PDF in UIDocumentInteractionController using bellow code This is a example of displaying pdf from NSBundle you can also displaying from Document directory like same as we display images. You need to full path of document and convert it into NSURL using fileURLWithPath:-
In yourViewController.h file:-
#interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *documentationInteractionController;
}
In **yourViewController.m file:-**
-(void) viewDidAppear:(BOOL)animated {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"abcd.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl];
documentationInteractionController.delegate = self;
[documentationInteractionController presentPreviewAnimated:YES];
[super viewDidAppear:animated];
}
Delegate of UIDocumentInteractionController
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
Demo link
happy coding... :)
Related
In one Of my activity I have generated pdf file on WebView and save this file in document folder successfully but this file path show for simulator then get pdf file easily, but when we run my application on iPhone simulator then file show on device successfully and give file path but I can't find file where it save how to get this file open another problem I am using this code.
NSMutableData *data2 = [[NSMutableData alloc] initWithCapacity:byteArray.count];
for (NSNumber *byteVal in self.WebService->byteArray)
{
Byte b = (Byte)(byteVal.intValue);
[data2 appendBytes:&b length:1];
}
NSString *resourceToPath = [[NSString alloc]initWithString:[[[[NSBundle mainBundle]resourcePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:#"Documents"]];
fileName = [resourceToPath stringByAppendingPathComponent:#"myPDF.pdf"];
[data2 writeToFile:fileName atomically:YES];
NSURL *targetURL = [NSURL fileURLWithPath:fileName];
NSURLRequest *requestFile = [NSURLRequest requestWithURL:targetURL];
UIWebView *webView1=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
webView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ;
webView1.scalesPageToFit = YES;
webView1.scrollView.scrollEnabled =NO;
[webView1 loadRequest:requestFile];
[self.view addSubview:webView1];
for download pdf file I am using this.....
-(void)DownloadPdffile
{
NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [docDirectories objectAtIndex:0];
NSString *filePAth = [docDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSURL *url2 = [NSURL filePAth];
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
docContr.delegate=self;
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
Using this code I've saved Pdf file in simulator successfully but in iPhone device I can't find where is my Pdf file and then using download pdf then .....this image open....!
But when I tapped any one of option then my application is closed so tell me how to open pdf in these option and save there also
If I understand what you are asking correctly, you need to refer to your documents folder differently in your -(void)DownloadPdffile method. Like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath [documentsDir stringByAppendingPathComponent:yourPdfFile.pdf];// your yourPdfFile file here
NSURL *url = [NSURL fileURLWithPath:pdfPath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController retain];
// docController is released when dismissed (autorelease in the delegate method)
BOOL isValid = [docController presentOpenInMenuFromRect:yourReadPdfButton.frame inView:self.view animated:YES]; // Provide where u want to read pdf from yourReadPdfButton
(from Prince's answer)
I am very new to iOS. I create PDF and load this PDF on UIWebView
now this time I want to save or download this PDF in iPhone when we tapped download button then all exits PDF supporter show like as open ibook ,open in chrome. This type of option show but when we tap any one then my application closed.
-(void)show_Button
{
NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [docDirectories objectAtIndex:0];
NSString *filePAth = [docDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSLog(#"filePath = %#", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(#"url2 = %#", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
docContr.delegate=self;
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
so how to save or download this pdf in Iphone please solve this problem....
I believe you can simple use the belo two line:
NSData *myFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your_url"]];
[myFile writeToFile:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"yourfilename.pdf"] atomically:YES];
I hope this it will help you,
Saving the pdf into app
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"pdfname.pdf"];
NSError *writeError = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];
if (writeError) {
NSLog(#"Error writing file: %#", writeError); }
Getting the pdf from the NSDocument Directory
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:&error];
for(int i=0;i<[filePathsArray count];i++)
{
NSString *strFilePath = [filePathsArray objectAtIndex:i];
if ([[strFilePath pathExtension] isEqualToString:#"pdf"])
{
NSString *pdfPath = [[stringPath stringByAppendingFormat:#"/"] stringByAppendingFormat:strFilePath];
NSData *data = [NSData dataWithContentsOfFile:pdfPath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[arrayOfImages addObject:image];
}
}
}
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”.
I tried to attach my pdf to an in-app-mail. The in-app-mail displays an icon with the pdf but it doesn't send it. I don't know why...
Here's the code:
- (void)openInEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.mailComposeDelegate = self;
[viewController setSubject:#"Stundenplan 1A"];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];
[viewController addAttachmentData:data mimeType:#"text/pdf" fileName:#"Stundenplan_1A.pdf"];
[self presentModalViewController:viewController animated:YES]; }
}
Any ideas?
Have you tried it with?
NSString *filePath = [documentsDirectory stringByAppendingFileComponent:#"%#/Stundenplan_1A.pdf"];
instead of
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
And instead of NSMutableData you could tried it with NSData
NSData *data = [NSData dataWithContentsOfFile:file];
Change the mime type like this.
NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"CheckList.pdf"];
Is it possible to download files implementing UI web iphone in my app? If so where would the files be stored and how can I access it?
You can't really download files simply by browsing to them but what you could do is use
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
to analyze the link for a file (say by looking at the extension of last path component) and if you want this kind of file to be downloaded than you could use [NSURLConnection connectionWithRequest:myURLRequest delegate:self]; and all its associated delegate methods to download and store the file in the documents folder.
if you want to visualize some known for webview file, it will show you automatically...but if page give back an unknown for webview file (This happened to me with Citrix ica file) webview will give you an error....to solve this problem i used this code (note that here i will allow to download only ica file, but you can change this condition):
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if([error code]!=102)
{ [self.lblError setText:[NSString stringWithFormat:#"%#",error]];
return;
}
NSDictionary *userInfo = [error userInfo];
NSString * url = [[NSString alloc] initWithFormat:#"%#",[userInfo objectForKey:#"NSErrorFailingURLKey"] ];
NSString *search = #"?";
NSRange result = [url rangeOfString:search];
NSInteger startingPosition;
NSString *fileName,*fileExtention,*fileLocation;
if (result.location != NSNotFound) {
startingPosition = result.location + result.length;
fileLocation = [url substringToIndex:result.location];
fileExtention=[fileLocation pathExtension];
}
else
{
fileLocation=url;
}
fileName = [fileLocation lastPathComponent];
fileExtention=[fileLocation pathExtension];
//check if file to download if ica file
if(![fileExtention isEqualToString:#"ica"])
return;
self.lblError.textColor=[UIColor blackColor];
self.lblError.text=[NSString stringWithFormat:#"downloading %#...",fileName];
NSURL * _url = [[NSURL alloc] initWithString:url];
// Get file online
NSData *fileOnline = [[NSData alloc] initWithContentsOfURL:_url];
// Write file to the Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
// NSLog(#"Documents directory not found!");
return;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
//NSLog(#"appFile path: %#",appFile);
[fileOnline writeToFile:appFile atomically:YES];
NSURL* aUrl = [NSURL fileURLWithPath:appFile];
self.interactionController = [UIDocumentInteractionController interactionControllerWithURL: aUrl];
self.interactionController.delegate = self;
self.lblError.text=[NSString stringWithFormat:#"%# downloaded",fileName];
[self.interactionController presentOpenInMenuFromRect:self.lblError.frame inView:self.view animated:YES];
}