iosswiftuiapplicationdelegateprotocol-extension

Extending UIApplicationDelegate Protocol


I would like to extend UIApplicationDelegate protocol and provide a default implementation for application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool method. However, default implementation I have provided does not get called.

Is it possible at all to extend UIApplicationDelegate protocol (with relation to UIApplication being a singleton, or protocol method being an optional), or am i doing something wrong?

thanks

AppDelegate.swift:

import UIKit
extension UIApplicationDelegate{
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        print("does not print anything on launch.")
        return true
    }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
}

Solution

  • turns out you can't provide default implementations for Objective-C protocols via extensions. See below link for detailed list of limitations on protocol extensions.

    https://www.captechconsulting.com/blogs/ios-9-tutorial-series-protocol-oriented-programming-with-uikit

    What we CAN'T do: Provide default implementations for Objective-C protocols.