首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS在另一个应用程序中打开PDF文件

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

Stack Overflow用户
提问于 2017-11-10 21:15:06
回答 1查看 1.1K关注 0票数 0

我想在其他应用程序中共享/打开我的应用程序中的PDF文件。

我想分享的两个主要应用程序是Dropbox和PDF Expert。

这是我使用的代码,但它不起作用。例如,如果我尝试通过airdrop、email和WhatsApp共享文件,它不会共享任何内容

代码语言:javascript
运行
复制
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?这样会更好,这样我就不需要在我的应用程序中下载文件,然后再分享它。

EN

回答 1

Stack Overflow用户

发布于 2017-11-10 21:47:35

尝尝这个。

代码语言:javascript
运行
复制
// 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];
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47223644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档