I use SSZipArchive. After downloading file I want to unzip archive and show image. But code doesn’t work. How to fix it?
download file
-(IBAction) downloadButton:(id)sender
{
if (_HighScore == 2) {
_url1 =[NSURL URLWithString:#"link2.zip"];
_downloadTask1 = [_session downloadTaskWithURL:_url1];
[_downloadTask1 resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
if (downloadTask == _downloadTask1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSURL *documentsFolder = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:#"file://%#/2.zip", documentsFolder]];
NSError *error;
[fileManager copyItemAtURL:location toURL:newLocation error:&error];
NSLog(#"file%#", newLocation.absoluteString);
}
unzip file
_documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
_zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:#"2.zip"];
_destinationPath1 = [_documentsDirectory1 stringByAppendingPathComponent:#"file://%#/2.zip"];
_fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_zipPath1 isDirectory:false];
if( [SSZipArchive unzipFileAtPath:_zipPath1 toDestination:_destinationPath1] != NO ) {
NSLog(#"Dilip Success");
}else{
NSLog(#"Dilip Error");
}
UPD
-(IBAction) downloadButton:(id)sender
{
if (_HighScore == 2) {
_url1 =[NSURL URLWithString:#"link2.zip"];
_downloadTask1 = [_session downloadTaskWithURL:_url1];
[_downloadTask1 resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
if (downloadTask == _downloadTask1) {
_documentsDirectory1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
_zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:#"2.zip"];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (downloadTask == _downloadTask1) { NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_zipPath1 atomically:YES];}
});
}
The problem is in saving zip file in DocumentDirectory, _filePath1 is contain the destination folder for unzip, instead of that you need to use filePath that contain zip file name along with path, so use that like this.
[urlData1 writeToFile:filePath atomically:YES];
Also it is batter if you use writeToFile:options:error: method so that you can know it is successfully writing Data or not.
NSError *error = nil;
[self.responseData writeToFile:zipPath options:0 error:&error];
Edit:
You are probably messing with something, so change your code from downloading to saving and unzipping like this.
-(IBAction) downloadButton:(id)sender
{
if (_HighScore == 2) {
_url1 =[NSURL URLWithString:#"link2.zip"];
_downloadTask1 = [session dataTaskWithURL:[NSURL URLWithString:#""] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Saved in NSDocumentDirectory
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *zipPath = [path stringByAppendingPathComponent:#"2.zip"];
[self.responseData writeToFile:zipPath options:0 error:&error];
//UNZip
NSString *zipPath1 = [path stringByAppendingPathComponent:#"2.zip"];
NSString *destinationPath = [NSString stringWithFormat:#"%#",path];
[SSZipArchive unzipFileAtPath:zipPath1 toDestination:destinationPath];
//Now access the content of zip
}];
[_downloadTask1 resume];
}
}
try like this , its work for me
[SSZipArchive unzipFileAtPath:filePath toDestination:outputPath delegate:self];
after completion of unarchive the following method will call
#pragma mark - Unzipp Delegate
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo: (unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath {
//write code here after unarchive zip file
}
Related
I'm receiving a String from a web service and I would like to store it as a pdf file.
I know the have asked before but the answer is always the same. I've tryed it and didn't worked, and it's not explained enough.
So, this is what I'm doing:
NSData *data = [documentString dataUsingEncoding:NSUTF8StringEncoding];
NSString *formattedName = [NSString stringWithFormat:#"%#.pdf", name];
NSString *pdfPath = [#"documents/" stringByAppendingPathComponent:formattedName];
NSError *error = nil;
if([data writeToFile:pdfPath options:NSDataWritingAtomic error:&error]) {
}
It's not entering in the if, and I think the file is not being generated.
I'm working with objective-c, xcode 9.
What should I modify?
Create it
NSError*error=nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"/Application Support"];
NSString*filePathC = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"historyImages"]];
if(![[NSFileManager defaultManager]fileExistsAtPath:filePathC])
{
if(
[[NSFileManager defaultManager] createDirectoryAtPath:filePathC
withIntermediateDirectories:YES
attributes:nil
error:&error])
{
NSLog(#"created");
}
else
{
NSLog(#"not created");
}
}
else
{
NSLog(#"exists");
}
///
NSData *data = [documentString dataUsingEncoding:NSUTF8StringEncoding];
NSString *formattedName = [NSString stringWithFormat:#"%#.pdf", name];
NSString *pdfPath = [filePathC stringByAppendingPathComponent:formattedName];
NSError *error2 = nil;
if([data writeToFile:pdfPath options:NSDataWritingAtomic error:&error2]) {
}
I have audio player. I download audio in this way
- (void) song{
if (_index == 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"1.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO];
if (!fileExists) {
NSString *stringURL = #"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
}
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
}
}
But when audio downloading I can’t do anything. User interface stop.
How can download audio and do anything in user interface simultaneously?
Give your method a block to execute when finished, then run the download code in the background. Lets say, for your case the output is a local file with the downloaded content:
- (void)songWithCompletion:(void (^)(NSString *))completion {
NSString *filePath = [self song];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(filePath);
});
}
Change the song method to return filePath. Don't ever call it directly, only via songWithCompletion.
- (void)song {
// ... your code from the OP
// don't allocate the audio player here, just return the local file
return filePath;
}
Call it like this...
[self songWithCompletion:^(NSString *filePath) {
if (filePath) {
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
// you should really handle audio player error here, too
} else {
// handle error
}
}];
You are downloading the file in the main thread. you need to make an asynchronous call in order to avoid stopping UI.
-(void)downloadAudio
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data] ;
} else {NSLog(#"no connection!");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(#"Succeed! Received %d bytes of data",[receivedData length]);
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSLog(#"%#", [documentPaths objectAtIndex:0]);
NSString *documentDirectoryPath = [documentPaths objectAtIndex:0];
NSString *folderPath = [documentDirectoryPath stringByAppendingPathComponent:#"audioFile.mp3"];
[receivedData writeToFile:folderPath atomically:YES];
NSURL *soundURL = [NSURL fileURLWithPath:folderPath];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];
player.volume=0.5;
NSError *error = nil;
if (!error) {
[player play];
NSLog(#"File is playing!");
}
else{
NSLog(#"Error in creating audio player:%#",[error description]);
}
}
else{
NSLog(#"File doesn't exist");
}
}
I am developing an iOS app. I download the text file from a URL and save it into a directory, but I can't find the file in the directory, so the code downloads it again. How do I check for the file?
if ([[NSFileManager defaultManager] fileExistsAtPath:localfile]) {
content = [NSString stringWithContentsOfFile:localfile
encoding:NSUTF8StringEncoding
error:NULL];
}else
{
NSURL* url = [NSURL URLWithString:#"http://webapp.opaxweb.net/books/gurugranthsahib.txt"];
NSArray* pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* documentsDir = [pathArray objectAtIndex:0];
localfile =[documentsDir stringByAppendingPathComponent:#"data"];
NSData* data = [NSData dataWithContentsOfURL:url];
[data writeToFile:localfile atomically:YES];
content = [NSString stringWithContentsOfFile:localfile
encoding:NSUTF8StringEncoding
error:NULL];
}
_textfield.text=content;
Your text data is quite big and hence you should use nsurl connection! Your code is totally correct but this is a better approach.
- (void)viewDidLoad
{
[super viewDidLoad];
self.fileName = #"Data";
self.fileData = [NSMutableData data];
[self checkFileExitsorNOt];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.fileData setLength:0];
self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.fileData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#", [dirArray objectAtIndex:0]);
NSString *path = [NSString stringWithFormat:#"%#/%#", [dirArray objectAtIndex:0],self.fileName];
if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) {
NSLog(#"writeToFile error");
}
else {
NSLog(#"Written!");
}
}
-(void) checkFileExitsorNOt
{
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#", [dirArray objectAtIndex:0]);
NSString *path = [NSString stringWithFormat:#"%#/%#", [dirArray objectAtIndex:0],self.fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSString *cont = [NSString stringWithContentsOfFile:localfile
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(#"Content from file : %#",cont);
}
else
{
NSString *file = [NSString stringWithFormat:#"http://webapp.opaxweb.net/books/gurugranthsahib.txt"];
NSURL *fileURL = [NSURL URLWithString:file];
NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
}
}
In my app i use the below code to wirte a .db file to Documents Directory and then want to use it
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#",str);
NSMutableDictionary *retval = [json objectWithString:str];
if ([[retval objectForKey:#"Success"] boolValue])
{
NSString *downloadedData=[[retval objectForKey:#"Value"] objectForKey:#"SqLiteFileArray"];
NSData *mydata=[NSData dataWithBase64EncodedString :downloadedData];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
NSString *documentFile=[documentDir stringByAppendingPathComponent:#"4_program.db"];
NSString * yourAppendingText=[[NSString alloc]initWithData:mydata encoding:NSUTF8StringEncoding];
[yourAppendingText writeToFile:documentFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(#"finish");
//[self getfile];
//NSLog(#"data %#",downloadedData);
}
else
{
NSLog(#"aaa");
}
self.view.userInteractionEnabled = TRUE;
}];
in nslog i see the finish statement so i thought the file written successfully
but when i re-enter to the app and check if file exists in documents directory it says it is not there
this is my file exists code:
NSString *docsDir;
NSString *realpath;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
realpath=[[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: #"4_program.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: realpath ] == NO)
{
NSLog(#"no file");
}
else
{
NSLog(#"file found");
[self getdata];
}
am i missing something? how can i learn file written successfully finished?
i changed to my writeToFile to
[[NSFileManager defaultManager] createFileAtPath:documentFile
contents:mydata
attributes:nil];
and now it works and everything is ok
here am creating the download task
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:#"http://app.msf.gov.sg/Portals/0/Summary/assistance/CSSD/ComCare%20#%20a%20Glance%20-%20English%20(final).pdf"]];
[downloadTask resume];
// Do any additional setup after loading the view from its nib.
}
and here am dowloading the pdf file successfully
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData *data = [NSData dataWithContentsOfURL:location];
dispatch_async(dispatch_get_main_queue(), ^{
[self.progressView setHidden:YES];
});
}
Now my question is that how can i store that pdf file and retrieve again
please help me
Thanks in advance..
Just move file from 'location' to your document directory:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *docFolder = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *fileURL = [docFolder URLByAppendingPathComponent:#"file.pdf" isDirectory:NO];
[fileManager moveItemAtURL: location toURL:fileURL error:nil];
NSLog(#" pdf file is here %#", fileURL);
We have to copy the location to our documents directory
NSError *error;
NSString *fileName = downloadTask.originalRequest.URL.lastPathComponent;
NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *loc = [docDir objectAtIndex:0];
NSString *filePathName = [loc stringByAppendingString:fileName];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:filePathName]) {
[fileMgr removeItemAtPath:filePathName error:&error];
}
BOOL success = [fileMgr copyItemAtPath:[location path] toPath:filePathName error:&error];
if (success) {
NSLog(#"FIle is successfully copied to documents directory");
}else{
NSLog(#"error:%#",error.description);
}