iOS:下载并保存文档中的PDF文件app
问题描述:
我想下载PDF文件,然后在webView
中打开它。我搜索了一下,然后我发现这个issu。接受的答案描述了如何下载和显示文件。当我测试它时,我无法在文档中找到该文件,因为它存储在我的应用程序中。此外,我不认为该文件被保存,因为即使通过要加载的文件路径,PDF也不会显示在webView中。这是答案的代码:iOS:下载并保存文档中的PDF文件app
// 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>)
什么是我做了什么问题?我怎样才能将下载的文件放在我的iPhone
的Documents
应用程序中,以便在用户希望像任何其他PDF一样时进行检查?
答
沙箱路径错误
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"Documents"
]];
请使用您的沙箱路径,而不是包路径。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
请问您能解释一下吗?我在哪里可以使用'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);'? –
试试这个.''' NSString * resourceDocPath = [NSString alloc] initWithString:[[NS [NSBundle mainBundle] [NSString allocDirectoryiesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];''' – iticle
替换你的NSString * ] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@“Documents” ]];''' – iticle