21

I want to know if I can call an app delegate method from another ViewController.

When the app starts, the application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool i method is called. Can I call this method a second time from another view controller?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Memon Irshad
  • 972
  • 1
  • 8
  • 17

5 Answers5

53

Not sure why you want to do this. You probably shouldn't, but for the purpose of answering the question here it is:

// get a reference to the app delegate
let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate

// call didFinishLaunchWithOptions ... why?
appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)
  • thahks for helping me – Memon Irshad Jun 08 '15 at 09:34
  • No problem. If you have time please explain why you want to call this function twice. In any case do read Ashish Kakkad's linked post. – Juan Carlos Ospina Gonzalez Jun 08 '15 at 09:37
  • when app first lunch then show terms & condition page when user click on agree then he can go next login vc and can login but user click on decline button then call next vc but user press login his goes to terms and condition page and agree condition – Memon Irshad Jun 08 '15 at 09:44
  • 1
    and in Swift4 use following let appDelegate = UIApplication.shared.delegate as! AppDelegate – Mashhadi Feb 07 '18 at 06:30
  • My understanding of this area is limited, so I'm just going to ask: this answer is creating a new AppDelegate object, and then making `appDelegate` a reference to it? – benc Jan 31 '23 at 19:43
25

In Swift 3.0, you can call as:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.anyAppDelegateInstaceMethod()
Tejinder
  • 1,507
  • 19
  • 22
8

This method is called just once when app launches. You can't from ViewController. Instead make user defined method in AppDelegete. and call that method from ViewController. By getting object of AppDelegate.

AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDel <Your method>];
sschunara
  • 2,285
  • 22
  • 31
  • There is a missing `[` in your first line. I guess this is the corrected line ? `AppDelegate *appDel = [(AppDelegate *)[UIApplication sharedApplication] delegate];` – Mymozaaa Feb 15 '16 at 19:25
1

Constructors:

Add a constructor in AppDelegate Class at the end of code

Swift 3.x

 class func shared() -> AppDelegate
{
    return UIApplication.shared.delegate as! AppDelegate
}

Swift 2.x

func appDelegate () -> AppDelegate
{
return UIApplication.sharedApplication().delegate as! AppDelegate
}

and add a var like this

var boolForFun = Bool()

How to use reference in your class?

Method

for swift 3x access functins or variables

AppDelegate.shared().boolForFun = true

for else

appDelegate().methodFoo()

Variable

appDelegate().foo
Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44
1

Swift 4, Swift 5

As others have said you shouldn't do that. It would be better if you trigger it when you are in a certain application life cycle and do something specific using the Notification Center.

Example (in ViewController):

NotificationCenter.default.addObserver(
    self,
    selector: #selector(applicationWillEnterForeground),
    name: UIApplication.willEnterForegroundNotification,
    object: nil
)

However, if you do have to call the app delegate method, you can use this

let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
Pengguna
  • 4,636
  • 1
  • 27
  • 32