1

使用此代码在(pdf文件)中单击打开后:

-(BOOL)application:(UIApplication *)application
       openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
    annotation:(id)annotation {
if (url != nil && [url isFileURL]) {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *filemgr;
    NSError *erf;
    filemgr = [NSFileManager defaultManager];
    if ([filemgr copyItemAtPath:[NSString stringWithFormat:@"%@", url] toPath:documentsDirectory error: &erf]  == YES)
        NSLog (@"Copy successful");
    else
        NSLog (@"Copy failed%@dest: %@", erf, url);
}
return YES;

}

我想将文件复制到我的应用程序,但我有这个错误:

错误域 = NSCocoaErrorDomain 代码 = 260 “操作无法完成。(可可错误 260。)” UserInfo = 0x200826c0 {NSFilePath = file://localhost/private/var/mobile/Applications/ * .pdf,NSUnderlyingError = 0x20082510 “操作无法完成。没有这样的文件或目录”} 什么?

4

2 回答 2

1

您不能将NSURL表示文件 URL 的文件转换为NSStringusing stringWithFormat:。您需要pathNSURL. 并且toPath:参数需要是包含文件名的完整路径,而不仅仅是您要复制到的目录。

NSString *sourcePath = [url path];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:[url lastPathComponent]];

if ([filemgr copyItemAtPath:sourcePath toPath:destPath error:&erf]) {
于 2013-08-18T18:58:28.847 回答
0

You probably want to think about moving that onto a background queue/thread because you are essentially locking up your app when it's called. This has two major side effects

  1. Your app will appear to have hung during the copy operation, which will not be great for the user experience.
  2. If it takes too long to do the copy, the app will not be able to respond to the OS and so the OS will think the app has hung forever and will kill it.

I would write a new method that takes a URL for copying and then you can do some unit testing against that method to make sure it's solid, before then scheduling it onto a queue/background thread in your application:openURL:sourceApplication:annotation:

于 2013-08-18T19:18:04.157 回答