transistorsoft / nativescript-background-fetch

iOS Background Fetch API Implementation for NativeScript
Other
28 stars 9 forks source link

Delegate Stealing #2

Open ShawnPavel opened 8 years ago

ShawnPavel commented 8 years ago

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.

christocracy commented 8 years ago

Thanks, I'll check it out.

christocracy commented 7 years ago

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.

JessicaBunyan commented 4 years ago

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