yataki yataki - 9 days ago 6
iOS Question

How to send data like NSString from AppDelegate.m to ViewController.m in Objective C

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.

Answer

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.