为什么在iOS 11中,我的UIApplicationState显示为非活动状态而不是活动状态?

时间:2017-09-25 13:50:28

标签: ios objective-c ios11 uiapplication

我已更新到最新的xcode - 9和iOS 11,现在我在UIApplicationState中面临问题。谁能告诉我为什么苹果在ApplicationState中做了这个改变。

例如:    在viewController viewWillAppear期间,我正在检查UIApplicationState:

     UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
        if (applicationState == UIApplicationStateActive)
        {
        }

但我得到applicationState = UIApplicationStateInactive。

或者我是否需要通过额外代码单独处理iOS 11版本:

  if (applicationState == UIApplicationStateActive || (@available(iOS 11, *) && applicationState == UIApplicationStateInactive)){
}

在所有版本中保持应用程序状态一致的任何其他答案?

提前致谢,

1 个答案:

答案 0 :(得分:4)

** 更新的答案 **

我使用以下代码测试iOS 10和iOS 11设备上的状态。

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(#function) active: \(UIApplication.shared.applicationState == .active)")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("\(#function) active: \(UIApplication.shared.applicationState == .active)")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("\(#function) active: \(UIApplication.shared.applicationState == .active)")

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
            print("\(#function) with delay active: \(UIApplication.shared.applicationState == .active)")
        }
    }
}

在iOS 10上运行时:

viewDidLoad() active: false
viewWillAppear active: false
viewDidAppear active: true // This differs from 11
viewDidAppear with delay active: true

在iOS 11上运行时:

viewDidLoad() active: false
viewWillAppear active: false
viewDidAppear active: false
viewDidAppear with delay active: true

我无法告诉你这是否是故意的,但事情确实发生了变化。也许可以在上面写一个雷达?