Search This Blog

Monday, 7 April 2014

Unzip File -iOS

Hello,

So , after a long research i found nothing which can unzip the zip file with subfolders and other files.So i am providing you a function which will do the job. Also, i assume you can setup the Objective-zip Library very well,for those who don't have the idea here is the link for tutorial and here is the link to download.


Below is the code , which is self explanatory, you just need your zip file Document directory root.


-(void)UnzipFile{
   
    NSArray *dirPaths;
    NSString *docsDir;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:[NSString stringWithFormat:@"Facility Manual.zip"]];
   
    ZipFile *zipF = [[ZipFile alloc]initWithFileName:soundFilePath mode:ZipFileModeUnzip];
    //Listing all files in zip file
   
    NSArray *infos = [zipF listFileInZipInfos];
    [loadingView setHidden:NO];
    bytesReceived.text  = @"Unzipping Files";
    [progress setHidden:YES];
    for(FileInZipInfo *fileInfo in infos){
       
        //Identifying if file in Zip is a folder or pdf
        NSRange range = [fileInfo.name rangeOfString:@".pdf" options:NSCaseInsensitiveSearch];
        if(range.length>0){
           
        //File found in zip locating the file in zip file
        [zipF locateFileInZip:fileInfo.name];
       
        //Read Stream reading current File in zip file
        ZipReadStream *readStream = [zipF readCurrentFileInZip];
       
        //Initializing and declaring buffer to read the data from zip file for current file
        NSMutableData *data = [[NSMutableData alloc]initWithLength:256];
       
        //Parsing the Name provided by zip file to get the file name
        NSArray *bits = [fileInfo.name componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
           
        //Building file path on local Drive where it will be unzipped.
        NSString *documentdir;
        NSArray *documentPaths;
         
        documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentdir = [documentPaths objectAtIndex:0];
           
        //Final path of file in local drive
        NSString *yoyoDir = [documentdir stringByAppendingPathComponent:[NSString stringWithFormat:@"All Files/%@",[bits objectAtIndex:bits.count-1] ]];
           
        //File Manager creating the file in local drive
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager createFileAtPath:yoyoDir contents:nil attributes:nil];
           
        //Writing data in file created on Local Drive
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:yoyoDir];
           
            do{
                //Reset Buffer
                [data setLength:256];
               
                int bytesRead = [readStream readDataWithBuffer:data];
                if(bytesRead>0){
                    [data setLength:bytesRead];
                        [fileHandle writeData:data];
                }else{
                    break;
                }
               

            }while(YES);
        //Releasing buffer and FileManager
        [fileHandle closeFile];
        fileManager = nil;
        data = nil;
           
        //Finishing the Read Stream reading.
        [readStream finishedReading];
        }
    }

    //Close the zipFile prevents from corrupt zip.
    [zipF close];
   
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    [fileMgr removeItemAtPath:soundFilePath error:NULL];
    [loadingView setHidden:YES];
   
}


Happy Programming ... ;)

Sunday, 23 March 2014

Notification issue -iOS

Hello Friends,

Seems  like Apple is not serious with there job ... ;) , i found another bug in iOS 7 , what happened was i triggered a local notification from my app only when my app is in background , local notification has Sound file which is supposed to be played when notification is delivered to user. And WHOA, when user taps on local notification and app becomes active, local notification sound kept on playing whereas it was supposed to be stopped. Then with little R&D , i found that this issue is with iOS 7 only and its working fine for previous versions.

       So enough talks ... phewww ...here is the function to fix this issue


//Fix Local Notification sound issue for iOS 7
- (void)fixLocalNotificationforiOS7{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    }
}



Happy programming... :)

CHAO...Signing out...

Tuesday, 11 March 2014

UITextView issue -iOS

Hello Friends,

                So here is another thing coming in iOS7 which may annoy you some day today or later....in iOS7 , if you guys noted or not your UITextView clips last line out of view. Try this thing tap on return until you reach end of UITextView bounds now again tap return and start typing ....voila where is the text !!!

             Actually it gets clipped , i am not sure if it is a bug or apple did this intentionally. But, the bottom line is  " It's Annoying" . Yesterday i wasted 2 hours to solve this issue :P

           And here is the code which may handle this thing at some extent.

NOTE: This code will clip the last line on tapping return , but as soon as user starts typing characters the line becomes visible :)


STEP 1: I assume you are using UITextViewDelegate in your view controller.

STEP 2: Simply over ride this below delegate function in your view controller.


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
   
   [textView scrollRangeToVisible:range];
    return YES;
}


I am simply trying to autoscroll the textview to make the range visible. I think its the easiest way to cope with it , until apple sort out this thing :)


Happy Programming!
CHAO

Wednesday, 5 February 2014

AVAudioPlayer issue -iOS

Hello All,

Today i found a strange behaviour of AVAudioPlayer in my app, i never set any property for microphone output or anything , and also i have multiple players on different View Controllers. But on one of them the Audio Output was played through Microphone instead of Speakers.

       So here is a simple code to route audio to speakers if its coming from Microphone using AVAudioPlayer.

Simple copy and Paste this below code in your : -(void)viewDidLoad; method:

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);


Hope this helps some one someday, can save your day ;)

CHAO

Happy Programming.