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.
Are there any tips on where I may be going wrong here?