2

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?
}
Fizz
  • 3,427
  • 4
  • 27
  • 43
user1994321
  • 81
  • 1
  • 8
  • is the app swift or obj-c at its creation? is that all the code in your extension? do you have a delegate instance set and what functions does it implement? – Wain Mar 29 '16 at 11:23
  • 1
    What are trying to accomplish with this, I'm curious? I don't see the point to give the appDelegate a default implementation since there is only 1 appDelegate. – crashoverride777 Mar 29 '16 at 11:27
  • 1
    @crashoverride777 i would like to develop a protocol that can be used in multiple projects with pods, which provides push notification related functionality (say UIPushNotificationDelegate). I would also like to extend UIApplicationDelegate protocol, provide a default implementation for didRegisterForRemoteNotificationsWithDeviceToken when implementer is UIPushNotificationDelegate, and send the device token to server using UIPushNotificationDelegate methods as default implementation. – user1994321 Mar 29 '16 at 11:38
  • @Wain I have added my entire AppDelegate.swift file. It was a Swift application from the beginning. Project code contains nothing but changes to my AppDelegate file and default files which come with swift Single Page application template. thanks. – user1994321 Mar 29 '16 at 11:42
  • This sounds interesting. I cant actually answer your question because I haven't use protocol extension for appDelegates yet so I am not sure if it actually works or if you made an error. – crashoverride777 Mar 29 '16 at 12:17
  • @crashoverride777 ok, thanks for your time! – user1994321 Mar 29 '16 at 12:19
  • Sorry. Your idea sounds interesting tho. If you manage to get it to work by yourself and dont have an answer posted yet please post your own answer for us to read. Thanks – crashoverride777 Mar 29 '16 at 12:22

3 Answers3

0

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.

user1994321
  • 81
  • 1
  • 8
0

I ran into the same problem and for this particular file I reverted to Objective-C to achieve this functionality.

#import <UIKit/UIKit.h>

@interface NSObject (BasicMethods) <UIApplicationDelegate>

@end

with

#import "UIApplicationDelegate+BasicMethods.h"

@implementation NSObject (BasicMethods)

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSLog(@"I'm getting called");
}

@end

works.

Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
-2

There is no use of doing extension for protocols.
Reasons are:

  1. Protocol contains only function declaration but extension needs function definition.
  2. Protocol is nothing but just set of rules(methods). It doesn't allocate any memory.
  3. Protocol function definitions will be in delegate class. So function call will never comes to function definition which you written in extension.
  • So then why does this functionality even exist in the language? Especially when we consider it wasn't there from the beginner. – nhgrif Mar 29 '16 at 12:58