1

我想从 webservice 下载 pdf 文件并用UIDocumentInteractionController打开它。我该怎么做?我使用以下代码进行下载

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"myurl/abc.pdf"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"abcd.pdf"];
pdfData writeToFile:filePath atomically:YES];

现在我应该怎么做才能用UIDocumentInteractionController打开它

4

1 回答 1

3

您可以使用以下代码打开 PDFUIDocumentInteractionController这是一个显示 pdf 的示例,NSBundle您也可以从 Document 目录显示,就像我们显示图像一样。您需要完整的文档路径并将其转换为NSURL使用fileURLWithPath:-

yourViewController.h文件中:-

@interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
     UIDocumentInteractionController *documentationInteractionController; 
}

在 **yourViewController.m文件中:-**

    -(void) viewDidAppear:(BOOL)animated {

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
            NSString *documentsDirectory = [paths objectAtIndex:0]; 
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"abcd.pdf"];

            NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
            documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl];
            documentationInteractionController.delegate = self;
            [documentationInteractionController presentPreviewAnimated:YES];

        [super viewDidAppear:animated];
    }

代表UIDocumentInteractionController

    - (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
        return self;
    }

演示链接 快乐编码... :)

于 2013-07-27T10:55:17.837 回答