Reputation: 1
I am trying to copy a file from my bundle to the documents directory in iOS with the following code.
code:
- (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];
}
}
Attach Log:
User Data: 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 = "Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\""; NSUserStringVariant = ( Copy ); }
The app has been released. Most phones are normal. Only some phones have this problem. Each log file reported errors is not the same file not exist.
SAMPLE:
NSError(NSCocoaErrorDomain:4)The file“contractpayment-detail.html”doesn’t exist。 NSError(NSCocoaErrorDomain:4)The file “vacating.png” doesn’t exist. NSError(NSCocoaErrorDomain:4)The file “Mode.ts” doesn’t exist. NSError(NSCocoaErrorDomain:4)The file “SystemCtrl.js” doesn’t exist.
"Why? How to fix it?"
Upvotes: 0
Views: 216
Reputation: 2786
plz try this code
//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;
}
here i am copying the images into MyFolder from Main bundle. you can give your zip files name and use this code.
Upvotes: 0