2016-09-07 115 views
0

在我的应用程序中,我需要允许用户浏览本地或文档目录中存储的一些PDF文件。从iPhone浏览PDF文件

想要放一个按钮,当用户点击该按钮时,它显示PDF文件并附加该文件并将其发送到服务器[例如:在Gmail中附加文档]。

我该如何实现this.help我。感谢提前。

在viewdidload /////下载文件目录中的pdf文件。

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]; 
    } 

在文档目录中获取文件。

-(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字符串得到该pdf文件如何显示在UICollectionView。帮我。

+0

您只能浏览.pdf目前在您的APP中。你知道自己在哪里.pdf,那么你的问题是什么? – Larme

+0

好的...该文件不在我的应用程序如何浏览文件 – saravanar

+0

你不能。它被称为沙箱,iOS没有“Finder.app”/ Explorer等效。看到那里:http://stackoverflow.com/questions/38873128/how-to-choose-pdf-file-in-ios – Larme

回答

1
  1. 添加此委托方法:UIDocumentInteractionControllerDelegate
  2. 使用方法如下:

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

希望这可以帮助你。如果您想从应用程序本身下载文件并将其存储在doc dir中,请告诉我。

+0

想要像图片库一样显示pdf文件。并选择一个pdf文件并上传到服务器 – saravanar

+0

1.尝试从doc目录中提取所有文件/从do目录中仅提取pdf文件。 2.过滤pdf,然后在集合视图中显示它。 3.当用户点击其中一个可以从集合视图委托方法中获得链接时,然后将该文件作为元数据上传到服务器。 –

+0

4.试试这个,用你的ext即pdf来过滤你得到的doc数组: - (NSArray *)findFiles:(NSString *)extension { NSMutableArray * matches = [[NSMutableArray alloc] init]; NSFileManager * fManager = [NSFileManager defaultManager]; NSString * item; NSArray * contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory()stringByAppendingPathComponent:@“Documents”] error:nil]; // >>>这个部分在这里将所有扩展名为 的文件添加到(内容中的项){if([item pathExtension] isEqualToString:extension]){[matches addObject:item]; } } return matches; } –

0

首先你需要在目录中获取可用的文件列表中,

  1. 这种方法可以帮助你找到指定路径上的文件。

    -(NSArray *)listAllFileAtPath:(NSString *)path{ 
    
    //----LIST OF AVAILABLE FILES----// 
    int count; 
    
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL]; 
    
    return directoryContent;} 
    
  2. 现在,您可以过滤PDF文件从它,那么你可以使用下面的链接附加文件并发送电子邮件。

  3. 请点击此链接iOS Email Attachment

+0

想要显示PDF文件像图片库。并选择一个PDF文件并上传到服务器 – saravanar

+0

我该如何调用此方法以及如何使用此方法.. – saravanar

+0

因此,您需要设计一个单独的视图,可以使用UICollectionView,它可以显示PDF的第一页的拇指图像和点击你可以发送电子邮件附件。 –