Create Global NSMutableArray, NSMutableDictionary,NSUserDefaults

Create a Class named globaldict Under the Subclass of NSObject In globaldict.h +(NSMutableDictionary *) somename; +(NSMutableArray*) somename1; +(NSUserDefaults*) somename2; In globaldict.m +(NSMutableDictionary*) somename { static NSMutableDictionary* theValue = nil; @synchronized([globaldict class]) // in a single threaded app you can omit the sync block { if (theValue == nil) { theValue = [[NSMutableDictionary alloc] init]; } } return theValue; } +(NSMutableArray*) somename1 { static NSMutableArray* theValue = nil; @synchronized([globaldict class]) // in a single threaded app you can omit the sync block { if (theValue ==…

Continue reading »

Send & Receive Data from Server

Type 1 : Send String to server Type 2 : Send JSON to server Type 3 : Send String to server Type 1 : Send String to server NSString *teststring = @”Sending this string to server”; NSURL *url = [NSURL URLWithString:@”http://…”]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:240.0]; NSData *requestData = [NSData dataWithBytes:[teststring UTF8String] length:[teststring length]]; [request setHTTPMethod:@”POST”]; [request setValue:@”text/plain” forHTTPHeaderField:@”Accept”]; [request setValue:@”text/plain” forHTTPHeaderField:@”Content-Type”]; [request setValue:[NSString stringWithFormat:@”%d”, [requestData length]] forHTTPHeaderField:@”Content-Length”]; [request setHTTPBody: requestData]; NSError *error; NSHTTPURLResponse *response; NSData *uData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *data=[[NSString alloc]initWithData:uData…

Continue reading »

Sava Data & Image Using NSUserDefaults

To save data NSString *savestring = text1.text; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:savestring forKey:@”savedstring”]; [defaults synchronize]; To load data NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *loadstring = [defaults objectForKey:@”savedstring”]; [label1 setText:loadstring]; To save image using NSUserDefaults, In ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIImageView *containedimage; IBOutlet UIImageView *newimage; } – (IBAction)load:(id)sender; – (IBAction)savebutton:(id)sender; @end In ViewController.m #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a…

Continue reading »