Friday, July 25, 2014

Check device OS version

Alertnative 1 :
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
           
if ([[vComp objectAtIndex:0] intValue] >= 7) {

      // OS 7 code here
}

Alertnative 2 :
NSString *reqSysVer = @"7.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    

if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {

     // OS 7 code here
}

Custom ActivityIndicator

//Display an Activity Indicator
-(void) displayActivityIndicator {

    // Set position for activityIndicator
    [self setActivityIndicatorPosition];
    
    [waitView setImage:[UIImage imageNamed:@"WaitScreen.png"]];
    
    activityIndicator= [[UIActivityIndicatorView alloc]
                        initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
    
    [activityIndicator setFrame:CGRectMake(86,30,30,30)];
    [activityIndicator startAnimating];
    
    [waitView addSubview: activityIndicator];
    
    [self.view] addSubview:waitView];
    
    [activityIndicator startAnimating];
    
    //Ignore interaction for background activities
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

}


#pragma mark -
#pragma mark setActivityIndicatorPosition
-(void) setActivityIndicatorPosition {
       
    CGRect screenSize = [[UIScreen mainScreen] bounds];      
    
     if( ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
        
        [waitView setFrame:CGRectMake(x,y,203,73)];
    }
    
    if(([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)) {
                
         [waitView setFrame:CGRectMake(x1,y1,203,73)];
    }    
}


//Hide Activity Indicator
-(void) hideActivityIndicator {
    
    //Hide waitView
    [waitView setImage:[UIImage imageNamed:nil]];
    [activityIndicator stopAnimating];
    
    if ([[UIApplication sharedApplication] isIgnoringInteractionEvents]) {
        
        // Start interaction with application
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }
       
}
 


NSUserDefault usage

 //Set NSUserDefault
NSString *somePersistantValue = @"null";
    
[[NSUserDefaults standardUserDefaults] setObject:somePersistantValue forKey:@"keyName"];

[[NSUserDefaults standardUserDefaults] synchronize];

 //Get NSUserDefault
NSString *fetchPersistantValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"keyName"]; 

Set notification for Orientation

//Set Notification on Orientation change
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:nil];


//  Handle orientation change
- (void)orientationChanged:(NSNotification *)notification {

    ...
}

Using camera / gallery of device

 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        
//camera selection
if(buttonIndex == 0){
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
                
[self presentViewController:imagePicker animated:YES completion:nil];                
}
else {
                
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:NSLocalizedString(@"DeviceItem", @"") delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                
[alert show];
}

}       
else if(buttonIndex == 1) {   //gallery selection
            
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
   imagePicker.delegate=self;
   [self presentViewController:imagePicker animated:YES completion:nil];
}

Execute code in background

// Fetch legend details task in background
                 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                     
                     // Perform task here

                 });

Check device orientation

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

 if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
     //Landscape mode
}
 else if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
     //Portrait mode
}