Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sharedApplication in Swift on iOS? [duplicate]

Tags:

ios

swift

How can I get instance of current UIApplication from View Controller in Swift?

I am getting Use of unresolved identifier 'sharedApplication' error with this code:

let app = sharedApplication as UIApplication
like image 537
Evgenii Avatar asked Jun 09 '14 06:06

Evgenii


2 Answers

EDIT Swift 4.x

let app = UIApplication.shared.delegate as! AppDelegate

If you're e.g. not in a ViewController, you must:

import UIKit

Perhaps try:

let app = UIApplication.sharedApplication()

EDIT (Swift 3):

It should be noted that in Swift 3, to accomplish this, you will want to access the shared property instead:

let app = UIApplication.shared
like image 120
Darren Avatar answered Oct 17 '22 04:10

Darren


You can do it by

let app =  UIApplication.sharedApplication()
like image 35
iPatel Avatar answered Oct 17 '22 03:10

iPatel