Nativescript iOS App委托方法未触发

时间:2019-07-17 14:00:24

标签: nativescript nativescript-angular

我想扩展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.tshttps://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的方法。

1 个答案:

答案 0 :(得分:0)

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

假设您要在main.ts之前的platformNativeScriptDynamic(...).bootstrapModule(...)中进行此操作。在执行ngOnInit时,将已经创建了默认的应用程序委托。