iOS:在文档应用程序中下载并保存PDF文件

iOS:在文档应用程序中下载并保存PDF文件,ios,objective-c,pdf,uiwebview,Ios,Objective C,Pdf,Uiwebview,我想下载一个PDF文件,然后在webView中打开它。我搜索了一下,然后找到了这个。接受的答案描述了如何下载和显示文件。当我测试它时,我在文档中找不到该文件,因为它存储在我的应用程序中。此外,我认为文件不会保存,因为即使我传递要加载的文件路径,pdf也不会显示在webView中。这是答案的代码: // Get the PDF Data from the url in a NSData Object NSData *pdfData = [[NSData alloc] initWithContent

我想下载一个PDF文件,然后在
webView
中打开它。我搜索了一下,然后找到了这个。接受的答案描述了如何下载和显示文件。当我测试它时,我在文档中找不到该文件,因为它存储在我的应用程序中。此外,我认为文件不会保存,因为即使我传递要加载的文件路径,pdf也不会显示在webView中。这是答案的代码:

// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
    NSURL URLWithString:@"http://www.example.com/info.pdf"]];

// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
        stringByAppendingPathComponent:@"Documents"
]];

NSString *filePath = [resourceDocPath 
    stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];


// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
这是我在日志中看到的:

libMobileGestalt MobileGestaltSupport.m:153: pid 2828 (my_app) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
2017-08-01 10:29:06.882562+0100 my_app[2828:1334057] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)
libMobileGestalt MobileGestaltSupport.m:153:pid 2828(my_应用程序)没有frZQaeyWLUvLjeuEK43hmg的沙箱访问权限,并且没有适当的权限
2017-08-01 10:29:06.882562+0100 my_应用程序[2828:1334057]libMobileGestalt MobileGestalt.c:550:无法访问InverseDeviceID(请参阅)
我做的有什么问题?如何将下载的文件放入我的
iPhone的
文档
应用程序中,以便在用户需要时像其他PDF一样进行检查?

沙盒路径错误

NSString *resourceDocPath = [[NSString alloc] initWithString:[
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
        stringByAppendingPathComponent:@"Documents"
]];
请使用您的沙箱路径而不是捆绑路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

你能再解释一下吗?我必须在哪里使用
NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)?试试这个。
NSString*resourceDocPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]替换您的
NSString*resourceDocPath=[[NSString alloc]initWithString:[[[[NSBundle mainBundle]resourcePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@“Documents”]你是对的!它很管用,谢谢!您能告诉我如何将PDF保存在iPhone中的文档或iBooks应用程序中吗?因此,用户可以在下载后随时阅读PDF,而无需打开我的应用程序?谢谢,我使用了UIDocumentInteractionController,我让用户选择要放置PDF的应用程序:)