如何在iOS上的Swift中获取sharedApplication?

时间:2014-06-09 06:27:16

标签: ios swift

如何在Swift中从View Controller获取当前UIApplication的实例?

此代码出现Use of unresolved identifier 'sharedApplication'错误:

let app = sharedApplication as UIApplication

2 个答案:

答案 0 :(得分:63)

编辑 Swift 4.x

let app = UIApplication.shared.delegate as! AppDelegate

也许试试:

let app = UIApplication.sharedApplication()
编辑(斯威夫特3):

应该注意,在Swift 3中,为了实现这一点,您需要访问shared属性:

let app = UIApplication.shared

答案 1 :(得分:10)

你可以通过

来完成
let app =  UIApplication.sharedApplication()