在哪里存储创建的PDF文件?

出现以下错误:

CGDataConsumerCreateWithFilename: failed to open `/name' for writing: Permission denied. 

使用此代码(不是我的):

 - (void)createPDFWithName { //NSURL * newFilePath = [[NSURL alloc] initFileURLWithPath:name]; NSString * newFilePath = @"name"; CGRect page = CGRectMake(0, 0, 300, 300); NSDictionary * metaData = nil; if (!UIGraphicsBeginPDFContextToFile(newFilePath, page, metaData )) { NSLog(@"error creating PDF context"); return; } UIGraphicsBeginPDFPage(); [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIGraphicsEndPDFContext(); } 

那么:我应该在哪里存储这个PDF文件? 我尝试了NSBundle MainBundle东西,它也NSBundle MainBundle

谢谢!

您应该阅读文件系统上可以创建文件的各个位置。 其中一个是文档路径,下面是一个例子。

 - (NSString *)documentPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return documentPath; } NSString * newFilePath = [[self documentPath] stringByAppendingPathComponent:@"name.pdf"]; 

扩展Alex的答案

文档目录是您应该考虑存储PDF的唯一位置。 它是唯一一个Apple保证应用程序创建的内容持久化并且在升级之间会持续存在的文档存储库等。

你不能将东西推入你的MainBundle,因为捆绑在你的应用程序的整个生命周期中保持不变