Friday, August 12, 2011

How to find the hardware version in iOS


+ (NSString *)getSysInfoByName:(char *)typeSpecifier {

size_t size;

sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);

char *answer = malloc(size);

sysctlbyname(typeSpecifier, answer, &size, NULL, 0);

NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];

free(answer);

return results;

}


// to get the hardware version, we need to pass the argument hw.machine


[ClassName getSysInfoByName:"hw.machine"];


//getSysInfoByName is class method so we need to call by class name


Determine device (iPhone, iPod Touch) with iPhone SDK

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])

// it is iphone
// similarly we can also check iPad,iPod, and simulator.

Tuesday, August 09, 2011

Download, Create and Display Image from URL

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];

UIImage *img=[UIImage imageWithData:data];

UIImageView* imageView = [[UIImageView alloc] initWithImage:img];

[self.view addSubview:imageView];


URL may be http//...../image.jpg

Tuesday, August 02, 2011

Create JSON Object Manually without using SBJSON in iOS SDK

+(NSString *)createJSONString:(NSDictionary *) dictionary

{

NSArray *jsonKeyArray = [dictionary allKeys];

NSArray *jsonKeyValue = [dictionary allValues];

NSString *jsonString = @"{";

for (int j=0; j<[jsonKeyArray count ]; j++) {

NSDictionary *dictFromArray = [jsonKeyValue objectAtIndex:j];

NSArray *keyArray = [dictFromArray allKeys];

jsonString = [NSString stringWithFormat:@"%@\"%@\":",jsonString, [jsonKeyArray objectAtIndex:j]];

NSArray *valueArray = [dictFromArray allValues];

// to check its empty dictionary

if ([keyArray count] != 0) {

for (int i = 0; i<[keyArray count];i++) {

if (i == 0) {

jsonString =[NSString stringWithFormat:@"%@{ \"%@\": \"%@\", ",jsonString,[keyArray objectAtIndex:i],[valueArray objectAtIndex:i]];

}//to check lost element of single dictionary

else if((i == [keyArray count]-1) ) {

if (j != [jsonKeyArray count]-1) {

// to check whether last element combined dictionary

jsonString =[NSString stringWithFormat:@" %@ \"%@\":\"%@\" },",jsonString,[keyArray objectAtIndex:i],[valueArray objectAtIndex:i]];

}else

{

jsonString =[NSString stringWithFormat:@" %@ \"%@\":\"%@\" }",jsonString,[keyArray objectAtIndex:i],[valueArray objectAtIndex:i]];

}

}else {

jsonString =[NSString stringWithFormat:@"%@ \"%@\":\"%@\", ",jsonString,[keyArray objectAtIndex:i],[valueArray objectAtIndex:i]];

}

}

}else {

if (j != [jsonKeyArray count]-1) {

jsonString =[NSString stringWithFormat:@"%@{},",jsonString];

}else

{

jsonString =[NSString stringWithFormat:@"%@{}",jsonString];

}

}

}

jsonString =[NSString stringWithFormat:@"%@ }",jsonString];

return jsonString;

}


simple star program in c

for(int i=0;i<10;printf("\n",i++))

for(int j=0;j<=10+i;printf("%c",(j++ < 10-i)?' ':'*'));



// another method to get


BOOL var = TRUE;

int n = 3;

for(int i=0;iprintf("\n",i++))

{

var = TRUE;

for(int j=0;j<= n+i;j++)

{

if (j >= n-i)

{

if (var == TRUE) {

printf("*");

var = FALSE;

} else {

var = TRUE;

printf(" ");

}

} else {

printf(" ");

}

}

}



Calendar Custom Event Creation in iOS SDK

EKEventStore *eventStore = [[EKEventStore alloc] init];

EKEvent *event = [EKEvent eventWithEventStore:eventStore];

event.title = @"EVENT TITLE";

event.startDate = [[NSDate alloc] init];

event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];

NSError *err;

[eventStore saveEvent:event span:EKSpanThisEvent error:&err];


Note: Please include Eventkit framework and include

#import EventKit/EventKit.h

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...