代码可以 100% 工作,但是在转移到 iOS7 和 XCode 5 后,我无法使用 NSFilemanager 方法获取文件的路径,代码找不到 timetableXML 文件的路径并触发 -> else @"NO 这样的文件存在”,这里有代码:
- (void)viewDidLoad
{
[super viewDidLoad];
APUAppDelegate *appDelegate = (APUAppDelegate *)[[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
NSLog(@"Beginning download");
NSString *stringURL = @"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSLog(@"Got the data!");
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSLog(@"Saving");
dataPath = [path stringByAppendingPathComponent:@"timetableXML.zip"];
dataPath = [dataPath stringByStandardizingPath];
[urlData writeToFile:dataPath atomically:YES];
/////////////////// TEST
// Existence of the xml file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathToMyFile = [path stringByAppendingPathComponent:@"timetableXML.zip"];
if ([fileManager fileExistsAtPath:pathToMyFile]) {
// file exists
NSLog(@"YES a file exist with ZIP extension");
NSLog(@"Unziped successfully!");
path1 = pathToMyFile;
[self unzipFile:path1];
}
else {
NSLog(@"NO such a file exist");
}
});
}