UIResponder doesNotRecognizeSelector: app is crashing with this error

763 Views Asked by At

I have crash for the app which is on App store.

Below is what I have in crash logs.

I have Base view controller in my project. I am extending each view controller with Base view controller.

In Base view controller, in viewDidAppear, I set the status bar color with below code.

static func adjustStatusBarToColor(colorAsString: String) {

    print("adjustStatusBarToColor===\(globalTopPadding)")

    if (globalTopPadding>=1) {
        if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
            let statusBar2: UIView = statusBar.subviews[0]
                let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")
                statusBar2.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
        }
    } else {
        if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
            let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")
            statusBar.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
        }
    }
}

Any idea why app is crashing?

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Check if element responds to specific selector before calling it, you will avoid any crashes.

let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")

if statusBar2.responds(to: setForegroundColor_sel) {
    statusBar2.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
}

I believe that this code returns some view that does not respond to changing color:

let statusBar2: UIView = statusBar.subviews[0]