Nativescript iOS 应用委托方法未触发

Nativescript iOS App Delegate Methods are not triggering

我想扩展 iOS 应用委托。我正在与一个名为 Mobile Pay (https://github.com/MobilePayDev/MobilePay-AppSwitch-SDK/wiki/Getting-started-on-iPhone) 的 SDK 集成,并且在打开外部应用程序进行支付时需要连接到 App Delegate。由于某些原因,在离开应用程序并打开移动支付应用程序时永远不会调用这些方法(例如 applicationHandleOpenURL)。

我一直在使用不同的示例,例如 nativescript-plugin-firebase 和 nativescript-urlhandler。也试过https://github.com/NativeScript/sample-ios-background-execution/blob/master/app/custom-app-delegate.ts and https://docs.nativescript.org/core-concepts/application-lifecycle

代码如下所示:

    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;
          };
    }

然后我的 package.json 在这里:

  "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"
  },

期待调用 applicationHandleOpenURL 的方法。

如果您在组件的 ngOnInit 中分配委托,那么这就是问题所在。

您应该在 platformNativeScriptDynamic(...).bootstrapModule(...) 之前的 main.ts 完成。到 ngOnInit 执行时,默认的应用委托已经创建。