Saturday 22 December 2012

Displaying Time as an integers in objective c


 NSDate *today = [NSDate date];
 NSCalendar *gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
 NSDateComponents *components =
[gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
    
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
    NSInteger second = [components second];

Wednesday 26 September 2012

JSON Parsing in iphone


Parsing the JSON objects in iphone using the default framework which was provided in ios5.
this framework is very simple to implement.
The response from the sample json is the string, arrays or Json objects.

The typical thing is the code changes based on the type of out put we get with the json.
The json response may be an array or string or a dictionary. so based on that we should perform the coding.

the following code replicates the json parsing.
   
//write this part of code in viewdidload:
 // h ah i have taken the url from the android tutorial
NSString *url=[NSString stringWithFormat:@"http://api.androidhive.info/contacts/"];
//getting the contents of url into data    
  NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    //using nsjsonserialisation
//the below part of code is used to take data fron NSData object and converts into string,
nsarray,NSDictionary based on options  NSJSONReadingMutableContainers then it returns the NSArray or NSDictionary
//if the options NSJSONReadingMutableContainers then it returns nsarray or dictioanry or if 
    NSDictionary *jsonse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"json response %@",jsonse);

 NSArray *subarr=[jsonse objectForKey:@"contacts"];
    NSLog(@"subarray %@",subarr);
    
    for (NSDictionary *d in subarr) {
        [names addObject:[d objectForKey:@"phone"]];  
        
        NSLog(@"names %@",names);
        for (NSDictionary *numbers in names) {
            NSLog(@"%@",[numbers objectForKey:@"mobile"]);
        }    }
    

Tuesday 25 September 2012

NSUser Defaults


NSUserdefault

hii NSUserDefault is the default class provided to store some information by default. The storage is permanent storage  but stores a very little content as a dictionary. It uses a key value pairing concept.

the sample code to store the data in NSUserdefaults is

-(IBAction)savetouserdefaults:(id)sender
{
    
    
    
    NSUserDefaults *defaultsstorage=[NSUserDefaults standardUserDefaults];
//text1.text is the my textfiled1 data    
    [defaultsstorage setObject:text1.text forKey:@"name"];
    [defaultsstorage setObject:text2.text forKey:@"lastname"];
    
    [defaultsstorage synchronize];
    

}


// to retrieve the data from nsuserdegfaults


prints the associated data with the keys
 NSUserDefaults *defauls=[NSUserDefaults standardUserDefaults];
    
    NSLog(@”%@”,[defauls objectForKey:@"name"]);
    NSLog(@” lastname %@”,[defauls objectForKey:@"lastname"]);