pbs5sq
Last Updated: February 25, 2016
·
619
· sendoa

Elegant global access to App Delegate

I know, I know… accessing the app delegate globally is not always a good idea but sometimes it's just the right thing to do. This is an easy and elegant way to access it from any file in our project:

AppDelegate.h

#import <UIKit/UIKit.h>

#define ApplicationDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)

@interface MyAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (copy, nonatomic) NSString* aString;

@end

Any other file on the project

NSString* myString = ApplicationDelegate.aString;