我正在尝试使用以下代码将文件从我的软件包复制到iOS中的文档目录。
代码:
- (NSString*)getModuleHome{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"webapp" ]stringByAppendingPathComponent:@"modules"];
return path;
}
- (void)FirstLoad{
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:[self getModuleHome]]) {
//建立目录
[[NSFileManager defaultManager] createDirectoryAtPath:[self getModuleHome] withIntermediateDirectories:YES attributes:nil error:nil];
}
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
BLYLogInfo(@"FirstLoad");
NSString *zipPath = [[NSBundle mainBundle] pathForResource:@"" ofType:@"zip"];
NSString *path = NSTemporaryDirectory();
NSString *dataPath = [path stringByAppendingPathComponent:@"modules.zip"];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
BLYLogInfo(@"fileExistsAtPath dataPath");
[[NSFileManager defaultManager] removeItemAtPath:dataPath error:&error];
}
if(![[NSFileManager defaultManager] copyItemAtPath:zipPath toPath:dataPath error:&error])
{
BLYLogError(@"Error copying files: %@", [error localizedDescription]);
[Bugly reportError:error];
}
BLYLogInfo(@"FirstLoad Unzip");
NSString *unZipPath = [path stringByAppendingPathComponent: @"modules"];
if ([[NSFileManager defaultManager] fileExistsAtPath:unZipPath]) {
[[NSFileManager defaultManager] removeItemAtPath:unZipPath error:&error];
}
[SSZipArchive unzipFileAtPath:dataPath toDestination:unZipPath];
BLYLogInfo(@"FirstLoad Copy");
NSString *destinationPath = [self getModuleHome];
BLYLogInfo(@"FirstLoad destinationPath:%@",destinationPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
BLYLogWarn(@"FirstLoad removeItemAtPath");
[[NSFileManager defaultManager] removeItemAtPath:destinationPath error:&error];
}
if (![[NSFileManager defaultManager] copyItemAtPath:unZipPath toPath:destinationPath error:&error]) {
BLYLogError(@"FirstLoad Error copying files: %@", [error localizedDescription]);
[Bugly reportError:error];
NSLog(@"FirstLoad Clean");
[[NSFileManager defaultManager] removeItemAtPath:dataPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:unZipPath error:nil];
//加载出错
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* view = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"抱歉,资源加载失败,请关闭重试"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[view show];
});
}
else{
BLYLogInfo(@"FirstLoad Clean");
[[NSFileManager defaultManager] removeItemAtPath:dataPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:unZipPath error:nil];
[userDef setObject:@"NO" forKey:@"UpdateFlag"];
[userDef synchronize];
[self FirstReload];
}
}
附加日志:
用户数据:NSErrorUserInfo:{NSDestinationFilePath = " /var/mobile/Containers/Data/Application/3575F3DD-4DC7-4472-A3D0-D4FF86C40BEF/Documents/webapp/modules/ihub/img/cat.png" ;; NSFilePath = " /private/var/mobile/Containers/Data/Application/3575F3DD-4DC7-4472-A3D0-D4FF86C40BEF/tmp/modules/ihub/img/cat.png" ;; NSSourceFilePathErrorKey = " /private/var/mobile/Containers/Data/Application/3575F3DD-4DC7-4472-A3D0-D4FF86C40BEF/tmp/modules/ihub/img/cat.png" ;; NSUnderlyingError ="错误域= NSPOSIXErrorDomain代码= 2 \"没有这样的 文件或目录\"&#34 ;; NSUserStringVariant =(复制); }
该应用已发布。 大多数手机都很正常。 只有部分手机有这个问题。 每个日志文件报告的错误都不是同一个文件不存在。
示例:
NSError(NSCocoaErrorDomain:4)的 文件“contractpayment-detail.html”不存在。 NSError(NSCocoaErrorDomain:4)文件“vacating.png”不存在。 NSError(NSCocoaErrorDomain:4)文件“Mode.ts”不存在。 NSError(NSCocoaErrorDomain:4)文件“SystemCtrl.js”不存在。
"为什么呢?如何解决?"
答案 0 :(得分:0)
试试这段代码
//array of main bundle images
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"admin_1454993109_restaurants_uploaded",
@"admin_1454993149_other_uploaded",
@"admin_1454993170_car_uploaded",
@"admin_1454993206_home_uploaded",
@"admin_1454993235_fashion_uploaded",
@"admin_1454993290_shirt_uploaded",
@"admin_1454993347_electronic_uploaded",
@"admin_1454993380_notes_uploaded", nil];
for (int i = 0; i < array.count; i++) {
// file URL in our bundle
NSURL *fileFromBundle = [[NSBundle mainBundle]URLForResource:[NSString stringWithFormat:@"%@",[array objectAtIndex:i]] withExtension:@"jpg"];
// Destination URL
NSString *path = [self MyImagesDocumentsDirectoryPath];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.jpg",path,[array objectAtIndex:i]];
NSURL *destinationURL = [NSURL fileURLWithPath:filePath];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
// copy it over
[[NSFileManager defaultManager]copyItemAtURL:fileFromBundle toURL:destinationURL error:&error];
NSLog(@"%@",error);
}
}
- (NSString *)MyImagesDocumentsDirectoryPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
return dataPath;
}
这里我将图像从主包中复制到MyFolder中。您可以提供您的zip文件名称并使用此代码。