0

In my application I need to allow the user to browse some PDF files from iPhone stored in local or document directory.

Want to put one button when user click that button its shows the pdf file and attach that file and send it server.[ex:attach document in gmail].

How can i achive this.help me. Thanks advance.

In viewdidload ///// download the pdf file in document directory.

NSString *stringURL = @"http://www.khazanah.com.my/khazanah/files/20/200f21f3-07ff-4903-ab99-7c0cb557eb51.pdf";


 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,@"filename.pdf"];

        NSLog(@"filePath %@",filePath);

        [urlData writeToFile:filePath atomically:YES];
    }

Get the file in in document directory.

-(IBAction)favbtnClicked:(id)sender
{
    NSString *searchFilename = @"filename.pdf"; // name of the PDF you are searching for

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

    NSString *documentsSubpath;
    while (documentsSubpath = [direnum nextObject])
    {
        if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
            continue;
        }

        NSLog(@"found %@", documentsSubpath);
    }


}

documentsSubpath in string got that pdf file how can i show in UICollectionView. help me.

saravanar
  • 639
  • 2
  • 11
  • 25

2 Answers2

1
  1. Add this delegate method: UIDocumentInteractionControllerDelegate
  2. Use it like this:

    UIDocumentInteractionController *viewer = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path_of_doc]]; viewer.delegate = self; [viewer presentPreviewAnimated:YES];

Hope this helps you. Let me know if you want to download the file from app itself and store it in doc dir.

Abhi
  • 267
  • 2
  • 4
  • want to show the pdf files like an image gallery. and pick one pdf file and upload to server – saravanar Sep 07 '16 at 11:46
  • 1. Try fetching all the files from doc dir/ fetch only pdf files from do dir. 2. filter for pdf, then show it in collection view. 3. when user will tap on one of those you can get the link from collection view delegate method, then upload the file as metadata to the server. – Abhi Sep 07 '16 at 12:41
  • 4. Try this, use your ext i.e. pdf to filter the array of doc you get: -(NSArray *)findFiles:(NSString *)extension{ NSMutableArray *matches = [[NSMutableArray alloc]init]; NSFileManager *fManager = [NSFileManager defaultManager]; NSString *item; NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil]; // >>> this section here adds all files with the chosen extension to an array for (item in contents){ if ([[item pathExtension] isEqualToString:extension]) { [matches addObject:item]; } } return matches; } – Abhi Sep 07 '16 at 12:44
0

First of all you need to fetch list of available files in directory,

  1. This method helps you to find files on given path.

    -(NSArray *)listAllFileAtPath:(NSString *)path{
    
    //----LIST OF AVAILABLE FILES----//
    int count;
    
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    
    return directoryContent;} 
    
  2. Now you can filter pdf files from it then you can use below link to attach file and send the email.

  3. Follow this link iOS Email Attachment

Samkit Shah
  • 721
  • 1
  • 7
  • 18
  • want to show the pdf files like an image gallery. and pick one pdf file and upload to server – saravanar Sep 07 '16 at 11:46
  • For that you need to design a separate view in that can use UICollectionView which can shows thumb image of first page of pdf and on click you can send email with attachment of it. – Samkit Shah Sep 07 '16 at 12:06
  • Here i edit my code see. how to save the pdf file and get. In that code documentsSubpath this string got pdf file how can i show in collection view help me. – saravanar Sep 07 '16 at 12:22
  • You can get thumbnail image of first page of pdf and display it on collectionview cell, then on click you can send emails. This [link](http://stackoverflow.com/questions/5658993/creating-pdf-thumbnail-in-iphone) will help you to get thumbnail image from pdf document. – Samkit Shah Sep 07 '16 at 12:40