【发布时间】:2021-06-16 04:00:03
【问题描述】:
如果我的应用程序被置于后台(有人单击主页按钮),然后重新打开,我想在我的 ViewController 中运行一个函数,我已经能够使用 willEnterForeground() 函数来获得它,但我需要在我的应用程序中打开的 url(假设我在 Mail 中,我打开一个 PDF,我说在我的应用程序中打开该 pdf),并使用我粘贴在下面的 func 应用程序,但是, willEnterForeground 函数在应用程序函数之前运行,我将在获取 url 之前运行我想要的方法。有没有办法可以在调用 willEnterForeground 函数之前以某种方式获取 url?
AppDelegate.swift
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
myUrl = url.path
return true
}
ViewController.swift
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: nil, object: nil)
}
func willEnterForeground(notification: NSNotification!) {
methodUsingUrl()
}
【问题讨论】: