我试图将NSMainBundle中的现有pdf文件复制到NSDocumentsDirectory中。稍后将此文件加载到uiwebview中。我似乎无法理解为什么它不复制文件。对此有何帮助?
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentsDirectory error:&error];
NSArray *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [path1 objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:@"document.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
答案 0 :(得分:2)
您的目标路径必须包含文件名。
来自文档:
dstPath
放置srcPath副本的路径。此路径必须包含新位置中的文件或目录的名称。此参数不得为零。
答案 1 :(得分:1)
使用以下内容替换您的代码来源:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"document.pdf"];
NSString *resourceDBBundlePath = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
[fileManager copyItemAtPath:resourceDBBundlePath toPath:documentDirectoryPath error:&error];
如FD_所述,您的目标路径需要包含您的文件名。