gpt4 book ai didi

iphone - UIDocumentInteractionController Controller 问题

转载 作者:行者123 更新时间:2023-12-01 19:09:05 25 4
gpt4 key购买 nike

我想从Web服务下载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

最佳答案

您可以使用波纹管代码以UIDocumentInteractionController打开PDF。这是显示NSBundle的pdf示例,也可以从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;
}

Demo link
快乐的编码... :)

关于iphone - UIDocumentInteractionController Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com