-4

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.

yataki
  • 13
  • 5

7 Answers7

1

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.

  1. Add a property in your AppDelegate.h

    @property (strong, nonatomic) NSString *strResponse;

  2. Add this in top of the AppDelegate.h (after import statement)

    #define theAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

  3. Update string value.

    Inside AppDelegate.m

    self.strResponse = @"some response";

    Outside AppDelegate.m

    theAppDelegate.strLoadApi = @"some good response";

  4. 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.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • what do you mean outside AppDelegate.m? – yataki Nov 30 '16 at 07:19
  • @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
0

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";
Jetpack
  • 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
0

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);
vegda neel
  • 154
  • 12
0

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);
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

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.

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
Chirag kukreja
  • 433
  • 2
  • 5
0

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

vaibby
  • 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
0
NSString *sData = @"data";
[[NSUserDefaults standardUserDefaults] setObject:sData forKey:@"keyName"];
[[NSUserDefaults standardUserDefaults] synchronize];
to get it back later

NSString *sData = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"keyName"];
memedina
  • 26
  • 3