0

I previously mocked the UIApplication in my app this way: How to mock UIApplication in Swift?

We now want to get iOS 10 devices to use open(_ url: URL, options: [String : Any] = [:], completionHandler completion: ((Bool) -> Swift.Void)? = nil) function as openURL(_ url: URL) -> Bool is deprecated. I don't seem to be able to mock this in the same way though.

Current Code

protocol BSApplicationProtocol {
    func openURL(url: URL) -> Bool

    @available(iOS 10.0, *)
    func open(_ url: URL, options: [String : Any], completionHandler completion: ((Bool) -> Swift.Void)?)
}

extension UIApplication: BSApplicationProtocol {
    internal func openURL(url: URL) -> Bool {
        return openURL(url)
    }

    @available(iOS 10.0, *)
    internal func open(_ url: URL, options: [String : Any], completionHandler completion: ((Bool) -> Swift.Void)?) {
        open(url, options: options, completionHandler: completion)
    }
}

func openAppSettings(application: BSApplicationProtocol = UIApplication.shared) {

    if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {        
        if #available(iOS 10.0, *) {
            application.open(settingsURL, options: [:], completionHandler: nil)
        } else {
             _ = application.openURL(url: settingsURL)
        }
    }
}

When running on an iOS 10 device, the open(_ url: URL, options: [String : Any] = [:], completionHandler completion: ((Bool) -> Swift.Void)? = nil) function is called but it ends up crashing in the UIApplication extension class with EXC_BAD_ACCESS.

In the screenshot attached below, you can see the old open function is in blue but the new one is green. This makes me think I am somehow referencing the function incorrectly.

enter image description here

Are there any tips on where I may be going wrong here?

Community
  • 1
  • 1
Hodson
  • 3,438
  • 1
  • 23
  • 51

1 Answers1

0

The solution was to give the function (in the BSApplicationProtocol) a different name.

open() --> open2() for example. (NOTE: You should probably come up with a better function name than this though)

Obviously then you have to change the function name in the extension and the function named called from within my openAppSettings function.

I'm not exactly sure why I didn't have to name openURL something different. The only difference I see between these two is that OpenURL returns a Bool and the new iOS 10 version has default values.

Hodson
  • 3,438
  • 1
  • 23
  • 51