Wednesday, 27 February 2013

To convert date format from string into any date format in IOS



NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            NSString* string=@"2013-2-12"; 
            NSDate *dat = [formatter dateFromString:string];
            
            [formatter setDateFormat:@"dd MM yyyy"];  // here u can change to get any format and print dat

How to know that the volume slider is vertical or horizontal and also transform view.


CGAffineTransform verticalTrans = CGAffineTransformMakeRotation(M_PI * 0.5);    CGAffineTransform horizontalTrans = CGAffineTransformMakeRotation(0);    if(!CGAffineTransformEqualToTransform(slider_answer.transform, verticalTrans))    
{           
slider_answer.transform = verticalTrans;        
slider_answer.frame=CGRectMake(60, 55, 40, 140);
// if coming view is not vertical we transform into vertical    
}   
else if (!CGAffineTransformEqualToTransform(slider_answer.transform, horizontalTrans))   
 {               
slider_answer.transform = horizontalTrans;       
slider_answer.frame=CGRectMake(30, 45, 120, 40);
// if coming one is not horizontal we change into horizontal  
  }

Monday, 25 February 2013

To set UIButton border width and color.


[[urButton layer] setBorderWidth:0.5f];
[[urButton layer] setBorderColor:[UIColor blackColor].CGColor];

Saturday, 23 February 2013

To open gallery or camera programmatically and get selected photo in delegate method in IOS


-(void)Open_Camera
{
    
    NSString *model = [[UIDevice currentDevice] model];
    if ([model isEqualToString:@"iPhone Simulator"] || [model isEqualToString:@"iPad Simulator"])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Angry!!!" message:@"No Camera found!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
       
       [self presentModalViewController: picker animated:YES];

        
        picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        
    }
}
#pragma mark Method_Open_Library
-(void)Open_Library
{
    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    // Set source to the camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate=self;
    // Delegate is self
    
    // Allow editing of image ?
    imagePicker.allowsEditing = YES;
    // Show image picker
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        CGRect popoverRect = CGRectMake(10, 10, 200, 200);
        
        [popover presentPopoverFromRect:popoverRect inView:self.contentView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
    }
    else
    {
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    
    
}

#pragma mark imagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    
    UIImage *image_answer = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];
   
  
}

To set limit for character input in UITextView and also edit by pressing back.


// Its a delegate just add this method for using.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text length] == 0)
    {
        if([textView.text length] != 0)
        {
            return YES;
        }
        else {
            return NO;
        }
    }
    else if([[textView text] length] > your limit value ) 
    {
        return NO;
    }
    return YES;
    
}

Wednesday, 20 February 2013

To find current latitude and longitude in IOS




First need to add core location.framework.

then add a instance variable as below:

 CLLocationManager *locationManager;

Then add below code :




locationManager = [[CLLocationManager alloc] init];
        locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        [locationManager startUpdatingLocation];
        latitude=[NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
        longitude=[NSString stringWithFormat:@"%f",locationManager.location.coordinate.longitude];
        
          NSLog(@"%@",latitude);
          NSLog(@"%@",longitude);


To make NSdictionary containing objects with nested NSdictionary


NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@"1" forKey:@"image"];


NSMutableDictionary *gpsDict = [NSMutableDictionary dictionary];
     [gpsDict setValue:@"lat" forKey:@"lat"];
     [gpsDict setValue:@"long" forKey:@"long"];

[dict setObject:gpsDict forKey:@"gps_data"];


OUTPUT:

{"image":"1","gps_data":{"lat":"lat","long":"long"}} 

To remove subviews excepting other views.


for (UIView *view in self.contentView.subviews) {
        if (![view isKindOfClass:[UIButton class]] && ![view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
    }

Monday, 18 February 2013

Convert Image to Base64 string in iOS

Add Base64 class www.imthi.com/wp-content/uploads/2010/08/base64.zip in your project Now encode as:


NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
 [Base64 initialize];
 NSString *strEncoded = [Base64 encode:data];
Decode as:
 [Base64 initialize]; 
 NSData* data = [Base64 decode:strEncoded ];;
 image.image = [UIImage imageWithData:data];

Saturday, 16 February 2013

Action Sheet with delegate


-(void)add_an_Image
{
    
    actionSheet_OpenImages=[[UIActionSheet alloc]initWithTitle:@"Select Image Source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Open Library",@"Camera", nil];
    [actionSheet_OpenImages setActionSheetStyle:UIActionSheetStyleBlackOpaque];
    [actionSheet_OpenImages showInView:self.view];
}





#pragma mark actionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    actionSheet_OpenImages.hidden=YES;
    
    switch (buttonIndex) {
         case 0:
            [self Open_Library];
            break;
        case 1:
            [self Open_Camera];
            break;
            
        default:
           break;
        }
    
    
}

Thursday, 14 February 2013

To find a specific attribute value for a entity from core data then we use NSpredicate for logical conditions.


 NSManagedObjectContext *context = [[SharedVars sharedInstance]localContext];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"formQuestionId == %i", i];
        [fetchRequest setPredicate:predicate];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"FormQuestion" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        
        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (FormQuestion *info in fetchedObjects)
        {
            
          //  [info setIsEnabled:[NSNumber numberWithInt:0]];
// edit and see data using info.yourAttributeId;
        }

To fetch the core data attribute value of a specific entity.




NSManagedObjectContext *context;
    
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FormQuestion" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    
for (FormQuestion *info in fetchedObjects)
    {
        int temp=[info.formQuestionId intValue];
        if(temp>LastQuestion)
        {LastQuestion=temp;}
    }

To reload table from cell creation



UITableView *parentTable = (UITableView *)self.superview;
[parentTable reloadData];

To set a entity attribute value from .m file where other attributes are dynamically filled.




-(id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
{
    self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
    if (self) {
        self.isEnabled = [NSNumber numberWithInt:1];
    }
    return self;
}