Declaring delegate methods in Cocoapod plugin


#1

I’m trying to incorporate a Cocoapod interface into the iOS version of my app: https://cocoapods.org/pods/ImagePicker

I’m confused about delegates. That cocoapod has 3 “delegate” methods:

func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage])
func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage])
func cancelButtonDidPress(_ imagePicker: ImagePickerController)

How do I declare those methods? Here is how I am presenting the view (after installing the cocoapod using these instructions: https://www.nativescript.org/blog/using-cocoapods-is-a-piece-of-cake-with-nativescript-1.3.0):

var pickerController = new ImagePickerController();
var iosPage = frameModule.topmost().ios.controller;
var self = page.ios;
pickerController.delegate = self;
pickerController.imageLimit = 1;
iosPage.presentViewControllerAnimatedCompletion(pickerController, true, null);

Ive tried an extremely wide variety of things, but couldnt hack it. Most of the time I get this error:

-[UIView doneButtonDidPress:images:]: unrecognized selector sent to instance 0x7fc4e9892830

The closest I got was with the following code. That would call doneButtonDidPress, but images would be undefined.

    var myPickerController = ImagePickerController.extend({
        viewDidLoad: function() {
            this.super.viewDidLoad();

            console.log('view load');
        },
        doneButtonDidPress(controller, images) {
            console.log(images);
            // Called when the extension is about to move from the inactive to active state.
            // This will happen when the extension is about to present UI.

            // Use this method to configure the extension and restore previously stored state.
        }
    }, {

        name: "myPickerController",
        exposedMethods: {
            "doneButtonDidPress": {
                returns: interop.types.void
            }
        }
    });


    var pickerController = new ImagePickerController();

#2

Delegates can be confusing. It’s arguably the hardest thing on iOS to work with when building a plugin, but once you’ve implemented it once you can repeat the pattern for all others.

You can copy-paste-adjust the code from two of my plugins if you like: nativescript-numeric-keyboard and nativescript-performance-monitor


#3

They are, indeed confusing. I’ve only been working with Typescript for a short while, and I’m having trouble going through the process of creating a seed. Eddy I watched your tutorial (https://www.youtube.com/watch?v=8sCd-rnifXM), and while it helped me understand how to build a plugin, I still cant figure out how to utilize the APIs provided by the cocoapod in the plugin. This, for example, works in mynoice-image-picker.ios.ts file:

import {Common} from './noice-image-picker.common';
export class NoiceImagePicker extends Common {
    constructor() {
        super();
    }

    public showPicker() {
        console.log('hi');
    }
}

Calling showPicker from the UI (a button) consoles ‘hi’. But this wont even compile:

import {Common} from './noice-image-picker.common';
export class NoiceImagePicker extends Common {
    constructor() {
        super();
    }

    public showPicker() {
        let pickerController = new ImagePickerController();
        pickerController.imageLimit = 1;
        console.dump(pickerController.imageLimit);
    }
}

Doing something something similar and utilizing the API that way in my app (see my original question) does work.


#4

Do you have a repo I can clone and fool around with?


#5

Here: https://github.com/davecoffin/noice-image-picker

Thanks!


#6

Work in progress, but getting there. Will do a PR to your repo:


#7

Awesome thank you!!! I owe you a beer. Were you able to wire up the delegate methods?


#8

:beers: delegate looks good. Will push in about an hour.


#9

Thanks for your help! Problem though, I get this when i run npm run demo.ios:

1   0x10366a7cb NativeScript::FFICallback<NativeScript::ObjCMethodCallback>::ffiClosureCallback(ffi_cif*, void*, void**, void*)
2   0x103d3f1ee ffi_closure_unix64_inner
3   0x103d3fb22 ffi_closure_unix64
4   0x104a5f10a -[UIViewController view]
5   0x10492763a -[UIWindow addRootViewControllerViewIfPossible]
6   0x104927d20 -[UIWindow _setHidden:forced:]
7   0x10493ab6e -[UIWindow makeKeyAndVisible]
8   0x103d3f97d ffi_call_unix64
9   0x1204e59d0
file:///app/tns_modules/nativescript-noice-image-picker/noice-image-picker.js:60:61: JS ERROR ReferenceError: Can't find variable: ImagePickerDelegate

Notice
JS ERROR ReferenceError: Can’t find variable: ImagePickerDelegate
Am I running the demo correctly?


#10

Hey Dave,

Looks like you need to remove and re-add node_modules (rm -rf node_modules && npm i) as well as the platforms/ios folder.

BTW, please just open an issue on GitHub and ping me there, otherwise I may miss your replies.


#11

Thanks so much Eddy you rock! You are now a contributor on https://www.npmjs.com/package/nativescript-noice-image-picker

I appreciate the help :slight_smile:


#12

Sweet plugin! Thank you for this @davecoffin and @Eddy!