I need to pass NSString data from AppDelegate.m inside didReceiveRemoteNotification method and send it to my ViewController. I need to put NSLog inside viewDidAppear every time i receive a notification.
-
Can you please more information about your problem? What did you try? – tuledev Nov 30 '16 at 06:52
-
1you can use NSUserDefault. – Saurabh Jain Nov 30 '16 at 06:57
-
I need to pass NSString data from AppDelegate.m inside didReceiveRemoteNotification method and send it to my ViewController. Display the data as a string somewhere in ViewDidApeear or ViewDidLoad NSLog(@"value: %@", getData); – yataki Nov 30 '16 at 06:59
-
@yataki try my answer its already i use in my project and working fine . if any query then tell me – Himanshu Moradiya Nov 30 '16 at 07:01
-
@Himanshu: where can i enter the first 3 lines of code? sorry, i dont know where. – yataki Nov 30 '16 at 07:09
-
add first 3 line of my answer in appdelagates.h file – Himanshu Moradiya Nov 30 '16 at 07:12
-
Possible duplicate of [Pass a value from one ViewController to another in Objective-C](http://stackoverflow.com/questions/20267387/pass-a-value-from-one-viewcontroller-to-another-in-objective-c) – Saranjith Nov 30 '16 at 07:30
-
@HimanshuMoradiya: i put comment in your answer – yataki Nov 30 '16 at 08:31
7 Answers
This is an expansion to @Himanshu's answer, if you're getting your NSString
in your AppDelegate
and it's something which you needs to preserve for further use, you can create a property and store it. There are multiple ways which you can follow to store / retrieve the values (example: NSDictionary
). So choose the one which will make your life easy in future.
Then you can get a shared object for your AppDelegate
and access to that property.
Adding his code to make this for future readers.
Add a property in your
AppDelegate.h
@property (strong, nonatomic) NSString *strResponse;
Add this in top of the
AppDelegate.h
(after import statement)#define theAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
Update string value.
Inside AppDelegate.m
self.strResponse = @"some response";
Outside AppDelegate.m
theAppDelegate.strLoadApi = @"some good response";
You can access it whenever requires.
NSLog(@"%@",theAppDelegate.strLoadApi);
P.S. This is not the best way to do achieve this. It's actually not recommended but I believe, you're learning iOS. So it's basic for you to start. However, you should read this answer to make your self ready for the future development.
-
-
@Hemang: There's nothing inside the string when i tried to NSLog inside viewdidappear of viewcontroller. – yataki Nov 30 '16 at 08:02
-
@Hemang: i already put some value inside AppDelegate.m but when i tried to access the data to know if there is an output there is nothing. i tried it in your way... – yataki Nov 30 '16 at 08:42
-
@Hemang: I'm sorry if you cant get my point. What i really want is to update my TableViewController every time i receive a notification. That's why I need to check inside viewDidAppear method and put some logs to know if my tableviewcontroller.m receive data from appdelegate.m... – yataki Nov 30 '16 at 09:00
-
Thx its working. But do you have have alternative way to update my TableView except this code? UINavigationController *navController = (UINavigationController *)self.window.rootViewController; MainTableViewController *notificationViewController = [[MainTableViewController alloc] init]; notificationViewController.recvString = str; [navController pushViewController:notificationViewController animated:YES]; – yataki Nov 30 '16 at 09:40
You can do this by adding a property to the header file of your ViewControler
@property NSString *data;
And then in your AppDelegate.m when you load your ViewController you can set the value of the property
self.YourViewController.data = @"Your String";

- 578
- 1
- 5
- 22
-
I need to pass NSString data from AppDelegate.m inside didReceiveRemoteNotification method and send it to my ViewController. Display the data as a string somewhere in ViewDidApeear or ViewDidLoad NSLog(@"value: %@", getData); – yataki Nov 30 '16 at 07:01
Interface:
@interface MyAppDelegate : NSObject {
}
@property (nonatomic, retain) NSString *myString;
...
@end
and in the .m file for the AppDelegate
you would write:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
self.myString = @" Here your String ";
return YES;
}
Then, in viewcontroller.m
file you can fetch:
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
someString = appDelegate.myString; //..to read
appDelegate.myString = someString; //..to write
NSLog(@"mystring is :- %@",appDelegate.myString);

- 154
- 12
-
would u add variable in app delegate for every value pass for every viewcontroller? – vaibby Nov 30 '16 at 07:10
-
i want the string data came from didReceiveRemoteNotification method of appdelegate... – yataki Nov 30 '16 at 07:49
add first 3 line in your AppDelegate.h file
#define theAppDelegate \
((AppDelegate *)[UIApplication sharedApplication].delegate)
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,nonatomic) NSString* strLoadApi; // create one property that you want to use in another viewcontoller.
Any viewcontoller you can set that value like and must be set before access / get value.
theAppDelegate.strLoadApi = @"Hello";
Any viewcontoller you can get that value like
NSLog(@"%@",theAppDelegate.strLoadApi);

- 4,769
- 4
- 25
- 49
-
would u add variable in app delegate for every value pass for every viewcontroller? – vaibby Nov 30 '16 at 07:11
-
yes you can pass it very where in controller but you have to write one line that NSLog(@"%@",theAppDelegate.strLoadApi); that print your appdelagates property value – Himanshu Moradiya Nov 30 '16 at 07:19
-
@HimanshuMoradiya: i tried this line of code NSLog(@"%@",theAppDelegate.strLoadApi); inside viewdidappear of viewcontroller it doesn't work. – yataki Nov 30 '16 at 07:42
-
did you assign or set value in appdelegates or any other controller before you get that value ? – Himanshu Moradiya Nov 30 '16 at 09:18
-
-
just update your your 2 controller method that you assign value and get value from appdelgates property . – Himanshu Moradiya Nov 30 '16 at 10:16
First, you need to take property in AppDelegate.h
@property(nonatomic) NSString *yourProperty;
Then in AppDelegate You assign the value to that property.
Then in your ViewController you can access that Property By:
AppDelegate * del = (AppDelegate*)[UIApplication sharedApplication]. delegate
NSString *str = del.yourProperty
Now use this str to display on UI.

- 3,748
- 2
- 30
- 55

- 433
- 2
- 5
-
would u add variable in app delegate for every value pass for every viewcontroller? – vaibby Nov 30 '16 at 07:09
whenever u want to pass NSString add this code
//find ur viewcontroller and set parameter
for (UIViewController *view in self.navigationController.viewControllers) {
if (view isMemberOfClass:[yourViewController class]) {
yourViewController *yview = (yourViewController *) view;
yview.parameter = @"string";
break;
}
}
in yourViewController set getter setter for parameter

- 1,255
- 1
- 9
- 23
-
What if it's a `UITabbarController` based or a drawer based application. You should update your answer for all the cases. – Hemang Nov 30 '16 at 07:16
-
However, your answer is not accurate, you're commenting everywhere about, taking more properties.. but what if, I need the same response in 10 view controllers? Will you add those properties in each view controller? – Hemang Nov 30 '16 at 07:21
-
@Hemang for tabbar controller you can say self.tabbarcontroller instead of self.navigationcontroller and it will work for drawer based application. – vaibby Nov 30 '16 at 10:11
NSString *sData = @"data";
[[NSUserDefaults standardUserDefaults] setObject:sData forKey:@"keyName"];
[[NSUserDefaults standardUserDefaults] synchronize];
to get it back later
NSString *sData = [[NSUserDefaults standardUserDefaults]
stringForKey:@"keyName"];

- 26
- 3
-
-
-
i tried that b4 its working but i need alternative way so that i can use it in the future when i want to pass a data like nsdictionary or nsarray... – yataki Nov 30 '16 at 07:33