4

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

Harsh Pipaliya
  • 2,260
  • 1
  • 14
  • 30
Gaurav Gudaliya
  • 131
  • 1
  • 1
  • 9

7 Answers7

10

Use like this:

In the AppDelegate.h

+ (AppDelegate *)sharedAppDelegate;

In AppDelegate.m

+ (AppDelegate *)sharedAppDelegate{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 1
    How 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
3

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

Community
  • 1
  • 1
Smile
  • 667
  • 1
  • 5
  • 21
1

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.

DilumN
  • 2,889
  • 6
  • 30
  • 44
1

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
Govindarao Kondala
  • 2,862
  • 17
  • 27
0

Use this:

+ (AppDelegate *)appDelegate
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48
0

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
Emma Labbé
  • 667
  • 7
  • 18
-3

Any ViewController it is working this appDelegate including Xcode 8 also

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
AnkiReddy
  • 1
  • 1