I have an iOS app written in Objective-C and I want to add an extension for UIApplicationDelegate using Swift. Specifically, I want to add code for the Objective-C method:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options;so that my app can handle an imported file.
In Swift, this method has the signature:
optional func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> BoolTo simplify the problem and I have created a Single View Application and have added the Swift code:
import Foundation
extension AppDelegate {
@objc public func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool {
return true
}
@objc public func testMessage() {
print("Test Message")
}
}This code compiles and runs, but does not get called which a file is imported. The MODULE-NAME-swift.h file conatins the following code:
...
@class UIApplication;
@interface AppDelegate (SWIFT_EXTENSION(AppDelegateTest))
- (BOOL)application:(UIApplication * _Nonnull)app open:(NSURL * _Nonnull)url options:(NSDictionary<NSString *, id> * _Nonnull)options;
- (void)testMessage;
@end
...This does not have the expected Objective-C signature and therefore, I assume is not called when a file is imported. Note that I can call both of these methods from Objective-C code,
If I change the signature in the Swift code to:
@objc public func application(_ app: UIApplication, openURL url: URL, options: [String : AnyObject] = [:]) -> Bool {
return true
}I get a compilation error:
Delegate.swift:13:24: Method 'application(_:openURL:options:)' with Objective-C selector 'application:openURL:options:' conflicts with method 'application(_:open:options:)' with the same Objective-C selector
__ObjC.AppDelegate:20:17: Method 'application(_:open:options:)' declared hereAny help would be appreciated.
David