Suraj Sukale Suraj Sukale - 2 years ago 114
Objective-C Question

How to access string (which is in view controller) from the app delegate

In view controller, i'm going to store parameters(

) in response object from server. i'm able to store that in 2 string e.g.
... like following

NSLog(@"Response from server 1 : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

NSError *error;
NSArray *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

// in this line we assingn the current value to global value
_tokenString = [[temp objectAtIndex:0] valueForKey:@"token"];
_edriveridString = [[temp objectAtIndex:0] valueForKey:@"edriverId"];

I need to know , How to access that 2 string (which is in view controller) from app delegate... i have no idea about this and i don't know to implement this..please help me.

and i want to access this two string from App delegate... how can i do this??

Answer Source


Why store those variables inside your class and then access it in app delegate when you can actually store those values directly in appDelegate ??? Create two public variables tokenString and edriveridString in appdelegate access appdelegate from your class which should not be difficult and then acees them directly in appdelegate :)

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.tokenString = [[temp objectAtIndex:0] valueForKey:@"token"];
appDelegate.edriveridString = [[temp objectAtIndex:0] valueForKey:@"edriverId"];

Now the data is with appdelegate access it whenever you want :) and bonus it is now accessible to all :) And AppDelegate is singleton as well :) so no fear of multiple instances of variables in memory :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download