在我的应用程序中,我需要允许用户从存储在本地或文档目录中的iPhone浏览一些PDF文件。
当用户点击该按钮时,想要放置一个按钮,它显示pdf文件并附加该文件并发送给服务器。[例如:在gmail中附加文件]。
我怎样才能实现这一点。帮助我。谢谢你。
在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 in string得到了pdf文件,我怎么能在UICollectionView中显示。帮助我。
答案 0 :(得分:1)
像这样使用:
UIDocumentInteractionController * viewer = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path_of_doc]]; viewer.delegate = self; [viewer presentPreviewAnimated:YES];
希望这会对你有所帮助。如果您想从应用程序本身下载该文件并将其存储在doc目录中,请告诉我。
答案 1 :(得分:0)
首先,您需要获取目录中的可用文件列表,
此方法可帮助您查找给定路径上的文件。
-(NSArray *)listAllFileAtPath:(NSString *)path{
//----LIST OF AVAILABLE FILES----//
int count;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
return directoryContent;}
现在您可以从中过滤pdf文件,然后您可以使用以下链接附加文件并发送电子邮件。
请点击此链接iOS Email Attachment