iOS在另一个应用程序中打开PDF文件

iOS在另一个应用程序中打开PDF文件,ios,objective-c,pdf,Ios,Objective C,Pdf,我想在另一个应用程序中共享/打开我应用程序中的PDF文件 我想与大家分享的两个主要应用是Dropbox和PDF Expert 这是我使用的代码,但它不起作用。例如,如果我试图通过空投、电子邮件和WhatsApp共享该文件,它不会共享任何内容 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory =

我想在另一个应用程序中共享/打开我应用程序中的PDF文件

我想与大家分享的两个主要应用是Dropbox和PDF Expert

这是我使用的代码,但它不起作用。例如,如果我试图通过空投、电子邮件和WhatsApp共享该文件,它不会共享任何内容

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf", @"test"]];
NSURL *URL = [NSURL fileURLWithPath:fullPath];
NSArray *activityItems = [NSArray arrayWithObjects:URL, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:nil];
test.pdf文件在我的项目中

是否有从服务器共享PDF的方法?这样更好,我就不需要在我的应用程序中下载文件,然后共享它。

试试这个

// In your header. MyViewController.h
@interface MyViewController : UIViewController 
                              <UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *docController;
}

// In your implementation. MyViewController.m Open Results is hooked up 
to a button.
- (void)openResults
{
 // Generate PDF Data.
 NSData *pdfData = [self makePDF];

 // Create a filePath for the pdf.
 NSArray *paths = 
 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
 NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *filePath = [documentsDirectory 
 stringByAppendingPathComponent:@"Report.pdf"];

 // Save the PDF. UIDocumentInteractionController has to use a physical 
 PDF, not just the data.
 [pdfData writeToFile:filePath atomically:YES];

 // Open the controller.
 docController = [UIDocumentInteractionController 
 interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
 docController.delegate = self;
 docController.UTI = @"com.adobe.pdf";
 [docController presentOpenInMenuFromBarButtonItem:shareButton 
 animated:YES];
 }
//在标题中。MyViewController.h
@接口MyViewController:UIViewController
{
UIDocumentInteractionController*文档控制器;
}
//在您的实现中。MyViewController.m打开的结果已连接
按一下按钮。
-(作废)公开结果
{
//生成PDF数据。
NSData*pdfData=[self-makePDF];
//为pdf创建文件路径。
NSArray*路径=
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*文件路径=[documentsDirectory
stringByAppendingPathComponent:@“Report.pdf”];
//保存PDF.UIDocumentInteractionController必须使用物理
PDF,而不仅仅是数据。
[pdfdatawritetofile:filePath原子:是];
//打开控制器。
docController=[UIDocumentInteractionController
interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docController.delegate=self;
docController.UTI=@“com.adobe.pdf”;
[docController PresentOpenMinumfromBarbuttonItem:shareButton
动画:是];
}

感谢他的回复,但您的代码缺少makePDF和shareButton。你能加上它们吗