Search code examples
iosobjective-ccocoa-touchpush-notification

Adding a category to AppDelegate to implement a method from UIAplicationDelegate protocol


I am developing a Cocoa Touch Framework, I want to receive remote notifications from APNS and I found that (after all the process it needs to establish communication with APNS) I have to catch notifications inside application:didReceiveRemoteNotification:fetchCompletionHandler: method. However, I want to let the less work possible to the IOS developer, so I been wondering.

  • Is it possible to create a category of the AppDelegate inside my framework (Since it already has an extension when a new app is created) in order to implement this method ?.

Solution

  • I would say it's not needed. What you can do here is create some function in your with the same params as in application:didReceiveRemoteNotification:fetchCompletionHandler: and make it available for the possible developer to call from your Framework.

    Many frameworks are working that way like FacebookSDK. For example, my code in AppDelegate for opening/handling URL:

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        let fbHandled: Bool = ApplicationDelegate.shared.application(app,
                                                                   open: url,
                                                                   sourceApplication: options[.sourceApplication] as? String,
                                                                   annotation: options[.annotation])
        return fbHandled
    }
    

    You could see that Facebook just provide me some public methods to handle links inside their SDK. It's a better solution than make implicit handling for something that the developer might be needed aware of and be available to change in his code.