【问题标题】:Swift: Reopen app from background, willEnterForeground runs before appdelegateSwift:从后台重新打开应用程序,willEnterForeground 在 appdelegate 之前运行
【发布时间】: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()
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    你的 AppDelegate.swift 应该有一个 applicationDidBecomeActive() 方法。根据苹果的文档,

    1. 第一个UIApplicationWillEnterForegroundNotification 通知被触发
    2. 第二,AppDelegate:applicationDidEnterForeGroundNotification被调用
    3. 三、AppDelegate:applicationDidBecomeActiveGetsCalled方法被调用
    4. 第四,UIApplicationDidBecomeActiveNotification 通知被触发

    因此,对于您的场景,将 url 收集在 AppDelegate:applicationDidEnterForeGroundNotification(或)AppDelegate:applicationDidBecomeActiveGetsCalled 中并让您的 ViewController.swift 监听 UIApplicationDidBecomeActiveNotification 是有意义的

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多