32

I need to Pass an string from App delegate to my Initial View Controller , Can somebody listed me the best way to do it , also i tried to Save and Retrieve using NS user Defaults, but i doesn't work out properly .

Alex
  • 511
  • 1
  • 7
  • 18
  • It's not for some reason. The reason is quite explicit in that the purpose of the app's delegate is to respond to changes in the app's state. Using it as a dumping ground for data and constants is poor form and leads to a dependency on an object that isn't a contributing citizen in the MVC paradigm. – Tyten Feb 10 '15 at 21:29

4 Answers4

30

Interface:

@interface MyAppDelegate : NSObject  {
  NSString *myString;
}
@property (nonatomic, retain) NSString *myString;
...
@end

and in the .m file for the App Delegate you would write:

@implementation MyAppDelegate
@synthesize myString;
    myString = some string;
@end

Then, in viewcontroller.m file you can fetch:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
someString = appDelegate.myString;  //..to read
appDelegate.myString = some NSString;     //..to write
Skanda
  • 872
  • 13
  • 21
  • 2
    This post just saved my day. Thanks a lot friend. – Avijit Jan 07 '14 at 11:47
  • 1
    Your answer should contain written in capital letters: "My method is dangerous and bad practice, because it breaks encapsulation and creates global state." – Tomasz Bąk Dec 17 '15 at 16:00
20

Here it is for Swift:

View Controller

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

Furthermore, if you have an object that you want to pass between view controllers (for example, I had CloudKit data I wanted to share) add this to the App Delegate:

    /* Function for any view controller to grab the instantiated CloudDataObject */
func getCloudData() ->CloudData{
    return cloudDataObject
}

Then back in the View Controller

var model : CloudData = self.appDelegate.getCloudData()
RyanPliske
  • 391
  • 3
  • 7
14

You can access your root view controller like this from the app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MyViewController* mainController = (MyViewController*)  self.window.rootViewController;
    [mainController passData:@"hello"];

    return YES;
}
Odrakir
  • 4,254
  • 1
  • 20
  • 52
3

Using Swift 4.2:

Passing Data from AppDelegate to ViewController:

let yourViewController = self.window?.rootViewController as? YourViewController
yourViewController?.passData(YOUR_DATA) // pass data
let data = yourViewController?.getData() // access data

Passing Data from ViewController to AppDelegate:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.passData(YOUR_DATA) // pass data
let data = appDelegate.getData() // access data

Add below code to YourViewController or AppDelegate:

private var data : String? // String? or any type you want
func getData() -> String? {
    return data
}
func passData(_ data : String?) {
    self.data = data
}
Mu Sa
  • 331
  • 3
  • 8