nativescript-background-fetch icon indicating copy to clipboard operation
nativescript-background-fetch copied to clipboard

Delegate Stealing

Open ShawnPavel opened this issue 9 years ago • 3 comments

In the readme for setup it is stated to do the following:

if (app.ios) {
  class MyDelegate extends UIResponder implements UIApplicationDelegate {
    public static ObjCProtocols = [UIApplicationDelegate];
      // BackgroundFetch delegate method
      public applicationPerformFetchWithCompletionHandler(application: any, completionHandler:any) {
        BackgroundFetch.performFetchWithCompletionHandler(completionHandler);
      }
    }
    app.ios.delegate = MyDelegate;
}

However, this has created some problems in my application. A lot of other plugins use the app delegate to perform numerous tasks. For instance the nativescript-firebase-plugin uses it to do google/facebook authentication and push notifications.

I have fixed this problem by utilizing the following code inside my Nativescript Angular app instead:

    if (application.ios) {
        application.ios.delegate.prototype.applicationPerformFetchWithCompletionHandler = function (application: any, completionHandler: any) {
            BackgroundFetch.performFetchWithCompletionHandler(completionHandler);
        };
    }

I took this method from the firebase plugin code. This allows other plugins to continue utilizing the app delegate without interruption. I don't know if it will work in base nativescript or not. It may be possible to incorporate this directly into the plugin (like the firebase plugin), but I am not sure.

ShawnPavel avatar Nov 14 '16 18:11 ShawnPavel

Thanks, I'll check it out.

christocracy avatar Nov 14 '16 18:11 christocracy

This is not "stealing" the AppDelegate. This plugin instructs you to implement the AppDelegate. This AppDelegate is an asset of your application, not the plugin.

Firebase is apparently checking for the existence of an implemented AppDelegate and adding their required method if it already exists.

christocracy avatar Nov 22 '16 18:11 christocracy

Just commenting to share knowledge as we ran into this same issue.

We used firebase plugin as well as this one and the firebase cloud notifications were completely stopped due to adding this plugin. From my understanding the following line

app.ios.delegate = MyDelegate;

Will override the functionality added to the delegate by nativescript-plugin-firebase.

I used the fix described in ShawnPavel's post above and both plugins appears to function now

JessicaBunyan avatar Aug 21 '20 11:08 JessicaBunyan