-1

In my storyboard I have set up a Facebook login that fetches the users username and profile picture. I have this all done in an initial UIViewController (AKA the LoginViewController).

What I want to do is to be able to send this data (the username and profile picture) to other places. More specifically, I want to be able to:

  • display the fetched profile picture in a UIImageView in a different UIViewController
  • display the username in another text field in a different UIViewController

If someone could simply just tell me how to do this either with a code example or even a storyboard walkthrough I would be very appreciative.

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42

1 Answers1

-1

Save the details you retrieved in LoginViewController using NSUserDefaults. It is system provided convenient storage which gives NSDictionary like access. Then you can retrieve details from other controllers.

To save from LoginViewController,

[[NSUserDefaults standardUserDefaults] setObject:@"user1" forKey:@"username"] ;
[[NSUserDefaults standardUserDefaults] setObject:UIImageJPEGRepresentation(image, 1.0) forKey:@"picture"] ;

To retrieve from another view controller,

NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"] ;
UIImage *image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"picture"]] ;
Trident
  • 810
  • 9
  • 20
  • This is a terrible idea. Definitely do not use NSUserDefaults to pass data around your app. It should be used to save data between app launches and updates. – Groot Apr 24 '15 at 22:23
  • It is designed to save data between app launches, I hope username and profile pictures are useful between app launches.. :). It is a convenient way to store data, it is thread safe too and there is no hard rule about what to save in NSUserDefaults. It does not affect the performance too, whats the issue? – Trident Apr 24 '15 at 22:27
  • This stores data and then reads it. It is not the same as passing data between view controllers which is what the OP is asking for. If you are using this pattern for passing data around in your app in the same session you are doing it wrong. NSUserDefaults should only be used for data that you want to be stored between app sessions. – Groot Apr 24 '15 at 22:32
  • Yes, we have to use design patterns to pass information from one controller to another controller, but we may have to save these details first some where, as we might launch the another view controller some time later, right.??. Of course, its always your choice to choose between NSFileManger and NSUserDefaults to save the data. – Trident Apr 24 '15 at 22:38
  • Only things that you want to keep between app launches/updates should be stored with NSUserDefaults (the OP has not said anything about this). The recommended pattern for passing data in one app during *one* session is described in the duplicate question I linked above. – Groot Apr 24 '15 at 22:43
  • He is talking about multiple view controllers, not just two view controllers, so we have to save details some where, so i was giving a lead in that direction. – Trident Apr 24 '15 at 22:50
  • Still, you do not need to, or more specifically, should not persistently store data that doesn't need to persist between app launches. If you need to access the same session data from different places in your app and doesn't want to pass references to it around you might consider using a singleton object. BUT never ever save data to disk that doesn't need saving. – Groot Apr 24 '15 at 23:11