1

I'm attempting to create a new plugin for Square Reader following the nativescript plugin seed (iOS instructions https://docs.connect.squareup.com/payments/readersdk/setup-ios). The last step is to pass in a UIViewController object so it can display the checkout page in your app. When trying to pass in the required parameters, I am consistently getting an error like this:

-[SquareReader checkoutController:didFailWithError:]: unrecognized selector sent to instance 0x10aa5c960 * JavaScript call stack: ( 0 UIApplicationMain@[native code] 1 start@file:///app/tns_modules/tns-core-modules/application/application.js:272:26 2 anonymous@file:///app/app.js:4:18 3 evaluate@[native code] 4 moduleEvaluation@:1:11 5 @:7:48 6 promiseReactionJob@:1:11 ) * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SquareReader checkoutController:didFailWithError:]: unrecognized selector sent to instance 0x10aa5c960' *** First throw call stack: (0x185676d8c 0x1848305ec 0x185684098 0x18567c5c8 0x18556241c 0x1019b21f4 0x184f68aa0 0x184f68a60 0x184f7565c 0x18561f070 0x18561cbc8 0x18553cda8 0x187522020 0x18f55c758 0x101640044 0x10163e7a4 0x10163e26c 0x100cf5630 0x1012f0e14 0x1012f9a24 0x1012f9a34 0x1012f2ee0 0x10128c198 0x101261e94 0x1013f6b9c 0x100d0a354 0x101492964 0x1012fa494 0x1012f9a34 0x1012f9a34 0x1012f9a34 0x1012f2ee0 0x10128c198 0x101261e94 0x1013f6c80 0x10148e8e0 0x100d01898 0x100d47f50 0x10079629c 0x184fcdfc0)

I'm trying to implement a UIViewController in my demo app:

export class HelloWorldModel extends UIViewController { ... }

The error comes from these lines:

let checkoutController = new SQRDCheckoutController( {parameters: params, delegate: this }); checkoutController.presentFromViewController(view);

where this is a SquareReader class object I made that has the function checkoutControllerDidFailWithError (that I need to have to implement SQRDCheckoutControllerDelegate) and view is an instantiation of the HelloWorldModel class.

I don't see anything for passing UIViewControllers to native iOS methods in {N} but I found https://discourse.nativescript.org/t/example-of-extending-uiviewcontroller-in-angular-2-and-nativescript/469 which might be similar?

Frank
  • 399
  • 4
  • 16

1 Answers1

1

Please refer the Objective C to JS docs to understand right way of calling native apis.

Assuming SQRDCheckoutController is the controller, the right way to initialise it with params would be

SQRDCheckoutController.initWithParametersDelegate(params, delegate)

If you use TypeScript, generate declaration files so it may easy for you to understand what are the JS form of calling available native api methods.

You may refer the tns core module for examples of UIViewController implementations. If you have plans to make this plugin open source, feel free to upload it to GitHub, so the plugin masters from community might be able to help you out easily.

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • Here's the repo: https://github.com/fhackenb/nativescript-square-reader I tried that line of code you provided and it ended with the same error. Now I'm thinking the error I'm getting now has to do with my delegate implementation since it isn't finding my `didFailWithError` method – Frank Oct 12 '18 at 15:13
  • You have to use `@ObjCClass(SQRDCheckoutControllerDelegate)` decorator on `SquareReader` class, so the runtime understands and maps the native delegate methods. Also move the declarations to separate typings files to it may be easy to read the actual code. – Manoj Oct 12 '18 at 20:23
  • The original library is in swift, so not sure if that makes a difference. When I add that line, I get this error: `error TS2693: 'SQRDCheckoutControllerDelegate' only refers to a type, but is being used as a value here` and when I change it to `@ObjCClass(SQRDCheckoutController)` or just `@ObjCClass()` I get `JS ERROR ReferenceError: Can't find variable: __metadata` – Frank Oct 15 '18 at 13:59
  • I just pushed them, thanks! I haven't moved the typings out yet, sorry - but I did figure out why `@ObjCClass(SQRDCheckoutControllerDelegate)` was throwing an error. Still getting the __metadata issue though – Frank Oct 15 '18 at 15:05
  • 1
    @Frank Please cleanup your repo and make sure at least it's running without tslint issues. The cleaner you keep it, easy for others to take a quick look. Also I don't get why you have to override the app delegate when you can simply get the built-in view controller and present your checkout controller. – Manoj Oct 17 '18 at 14:56
  • thanks for all the help. Was able to get it figured out and finish the plugin. My typings were screwed up and I hadn't realized I could just pass `page.ios` as a `UIViewController` (https://market.nativescript.org/plugins/nativescript-square-reader)[https://market.nativescript.org/plugins/nativescript-square-reader] – Frank Oct 19 '18 at 13:37