I am using AFHTTPRequestOperation
to download a large file to the Documents directory on my device.
NSURLRequest *request = [NSURLRequest requestWithURL:vectorFile.url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *path = [self pathForFileName:vectorFile.fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
progress(percentDone, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *path = [self pathForFileName:vectorFile.fileName];
extracting();
[SSZipArchive unzipFileAtPath:path toDestination:[self path] progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
NSLog(@"Unzipping");
} completionHandler:^(NSString *path2, BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Successfully downloaded file to %@", path);
dispatch_async(dispatch_get_main_queue(), ^{
completion(YES);
});
} else {
NSLog(@"%@", error);
failure(error);
}
}];
});
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSLog(@"%@", error);
failure(error);
}];
[operation start];
For some strange reason, it works on the Simulator to download the files, but on the device (iPhone 6) I get the following error:
Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"
The URL is correct, as it downloads in my browser correctly and in the Simulator. Why would this just be happening on the device? And what could be causing it?
I have tried restarting my device and resetting network settings.