gpt4 book ai didi

ios - 在模拟器中使用 UIDocumentInteractionController

转载 作者:行者123 更新时间:2023-11-29 04:16:01 29 4
gpt4 key购买 nike

我对 UIDocumentInteractionController 非常绝望。我唯一想做的就是与注册 PDF 的所有其他应用程序共享我的应用程序中的 pdf 文件。

这是我的代码:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.docController.delegate = self;

NSURL *url = [NSURL fileURLWithPath:filePath];
BOOL isValid = [[UIApplication sharedApplication] canOpenURL:url];
NSLog(@"uti: %@", [self.docController UTI]);

//[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
[self.docController presentOpenInMenuFromBarButtonItem:self.backBut animated:YES];

isValid 变量始终为 TRUE,UTI 记录为 com.adobe.pdf (这似乎是正确的),但调用 presentOpenFrom... 两个调用(其中一个已评论)始终返回 NO。

我正在用模拟器测试,这是问题吗?有人发现代码有问题或者知道要检查什么吗?

最佳答案

如果您正在处理 PDF 文件,那么您应该有任何能够在您的设备(模拟器)上打开 PDF 文件的应用程序。您可能需要安装PDF File Viewer因为它是开源的。通过从 Xcode 运行安装应用程序后,您的 UIDocumentInteractionController 将正确显示。

这里是一个示例代码,用于从网络下载任何文档并提供 UIDocumentInteractionController 将其传递给另一个应用程序。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:url.lastPathComponent];
NSData *fileData = [NSData dataWithContentsOfURL:url];
[fileData writeToFile:filePath atomically:YES];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePathURL];
[self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

关于ios - 在模拟器中使用 UIDocumentInteractionController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701900/

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