bbl64
bbl64

Reputation: 55

Nativescript iOS App Delegate Methods are not triggering

I'm wanting to extend the iOS app delegate. I'm integrating with a SDK called Mobile Pay (https://github.com/MobilePayDev/MobilePay-AppSwitch-SDK/wiki/Getting-started-on-iPhone) and need to hook into the App Delegate when opening an external app for payment. For some reason the methods (such as applicationHandleOpenURL) are never called when leaving the app and opening the mobile pay app.

I've been using different examples such as nativescript-plugin-firebase and nativescript-urlhandler. Also tried https://github.com/NativeScript/sample-ios-background-execution/blob/master/app/custom-app-delegate.ts and https://docs.nativescript.org/core-concepts/application-lifecycle

The code looks like this:

    private getAppDelegate() {
        // Play nice with other plugins by not completely ignoring anything already added to the appdelegate
        if (iosApp.delegate === undefined) {

          @ObjCClass(UIApplicationDelegate)
          class UIApplicationDelegateImpl extends UIResponder implements UIApplicationDelegate {
          }

          iosApp.delegate = UIApplicationDelegateImpl;
        }
        return iosApp.delegate;
    }

    private addDelegateMethods() {
        let appDelegate = this.getAppDelegate();

        console.log("er are adding this stuff to the equation lol");
          appDelegate.prototype.applicationDidFinishLaunchingWithOptions = (application, launchOptions) => {
            console.log("we are here or did finish?");
            return true;
          };

          appDelegate.prototype.applicationHandleOpenURL = (application: UIApplication, url: NSURL): boolean => {
            console.log("we are here or what?");
            MobilePayManager.sharedInstance().handleMobilePayCallbacksWithUrlSuccessErrorCancel(
                url, this.onPaymentSuccess, this.onPaymentFailure, this.onPaymentCancel);
            return true;
          };

          appDelegate.prototype.applicationOpenURLOptions = (app: UIApplication, url: NSURL, options: NSDictionary<string, any>): boolean => {
            console.log("we are here or what?");
            MobilePayManager.sharedInstance().handleMobilePayCallbacksWithUrlSuccessErrorCancel(
                url, this.onPaymentSuccess, this.onPaymentFailure, this.onPaymentCancel);
            return true;
          };

          appDelegate.prototype.openURL = (url: NSURL): boolean => {
            console.log("we are here or what?");
            MobilePayManager.sharedInstance().handleMobilePayCallbacksWithUrlSuccessErrorCancel(
                url, this.onPaymentSuccess, this.onPaymentFailure, this.onPaymentCancel);
            return true;
          };

          appDelegate.prototype.applicationOpenURLSourceApplicationAnnotation = (application: UIApplication, url: NSURL, sourceApplication: string, annotation: any): boolean => {
            console.log("we are here or what?");
            MobilePayManager.sharedInstance().handleMobilePayCallbacksWithUrlSuccessErrorCancel(
                url, this.onPaymentSuccess, this.onPaymentFailure, this.onPaymentCancel);
            return true;
          };
    }

and then my package.json is here:

  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "nativescript-angular": "~7.2.0",
    "nativescript-mobilepay": "1.0.5",
    "nativescript-theme-core": "~1.0.4",
    "reflect-metadata": "~0.1.12",
    "rxjs": "~6.3.0",
    "tns-core-modules": "~5.3.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~7.2.0",
    "@nativescript/schematics": "~0.5.0",
    "@ngtools/webpack": "~7.2.0",
    "nativescript-dev-typescript": "~0.9.0",
    "nativescript-dev-webpack": "~0.21.0"
  },

Expecting in method to be called applicationHandleOpenURL.

Upvotes: 0

Views: 1407

Answers (1)

Manoj
Manoj

Reputation: 21908

If you are assigning the delegate in your component's ngOnInit, then that's the issue here.

You are suppose to do it in main.ts before platformNativeScriptDynamic(...).bootstrapModule(...). By the time ngOnInit is executed, the default app delegate would have been already created.

Upvotes: 2

Related Questions