How to create sharedInstance
of AppDelegate
and use in my Application anywhere.

- 2,260
- 1
- 14
- 30

- 131
- 1
- 1
- 9
-
use this macro #define myappDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate]) – Smile Dec 23 '15 at 10:39
-
Do you need in objective C or swift? – Govindarao Kondala Dec 23 '15 at 10:40
-
AppDelegate *appdelegate =((AppDelegate *)[[UIApplication sharedApplication] delegate]) – Smile Dec 23 '15 at 10:40
-
AppDelegate is already shared throughout the app, you can use it's instance like [[UIApplication sharedApplication] delegate]; – channi Dec 23 '15 at 10:40
-
2Did you even search for it? – Ankit Srivastava Dec 23 '15 at 11:01
7 Answers
Use like this:
In the AppDelegate.h
+ (AppDelegate *)sharedAppDelegate;
In AppDelegate.m
+ (AppDelegate *)sharedAppDelegate{
return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- 46,283
- 15
- 111
- 140
-
1How to deal with the info that appears on the return line? _[UIApplication delegate] must be used from main thread only._ – Yuliwee Oct 30 '18 at 09:27
declare the statement in Constants.h
#define myappDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
and you can use myappDelegate
any where in the app if you declare the Constants.h in .pch file
Check this for PCH for Xcode 6
Use this in every ViewControllers
#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Or you can create a method in ViewController subclass & use it in every ViewControllers.
-(AppDelegate*) app
{
return (AppDelegate*) [[UIApplication sharedApplication] delegate];
}
In a subclass of UIViewController -- then make that the base-class of all of your view controllers.
Then, [self app] works, and you don't have to keep a reference.

- 2,889
- 6
- 30
- 44
you can use like bellow
In Objective C : you can add following line in your .pch file and user where ever you want
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
and use like myAppDelegate.someVariable or [myAppDelegate somemethod]
In Swift
Swift >= 1.2 (Introduced with Xcode 6.3)
let myAppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let aVariable = myAppDelegate.someVariable
Swift < 1.2
let myAppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let aVariable = myAppDelegate.someVariable

- 2,862
- 17
- 27
Use this:
+ (AppDelegate *)appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- 9,286
- 3
- 31
- 48
Other answers use UIApplication.shared.delegate, but you need to write always 'as! AppDelegate', so a Swift more clean code:
// AppDelegate.swift
static var shared = AppDelegate() // Declare a static variable as an instance of AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Singleton
AppDelegate.shared = self // set the value of "shared" to the current instance
return true
}
Usage
AppDelegate.shared

- 667
- 7
- 18