0

I'm writing nativescript plugin for image picker. I'm finished with part of android. Now, I'm writing code for ios. It's showing image picker dialog, but assigned delegates are not getting triggered. Please check my code below.

import * as application from "tns-core-modules/application";
import * as frame from "tns-core-modules/ui/frame"
export class Nativemediapicker extends NSObject implements UIImagePickerControllerDelegate {
    public static ObjCProtocols = [UIImagePickerControllerDelegate];
    get() {
        let version = NSBundle.mainBundle.objectForInfoDictionaryKey("CFBundleShortVersionString");
        return version;
    }
    static new(): Nativemediapicker {
        return <Nativemediapicker>super.new();
    }
    private _callback: (result?) => void;
    private _errorCallback: (result?) => void;
    public initWithCallbackAndOptions(callback: (result?) => void, errorCallback: (result?) => void, options?): Nativemediapicker {
        this._callback = callback;
        this._errorCallback = errorCallback;
        if (options) {
            // collect options
        }
        console.log('initWithCallbackAndOptions')
        return this;
    }
    static registerFileProvider(provider) { }
    static pickFiles(mimeType, onResult, onError) {
        onError("ERROR: For ios this feature is comming soon.");
    }
    static takePicture(onResult, onError) {
        // if (!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
        //     onError("ERROR: For ios simulator this feature is not supported.");
        //     return
        // }
        let imagePicker = UIImagePickerController.new()
        imagePicker.delegate = Nativemediapicker.new().initWithCallbackAndOptions(onResult, onError, null)
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.allowsEditing = false
        // imagePicker.showsCameraControls = true
        let topMostFrame = frame.topmost();
        if (topMostFrame) {
            let viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
            if (viewController) {
                while (viewController.parentViewController) {
                    // find top-most view controler
                    viewController = viewController.parentViewController;
                }

                while (viewController.presentedViewController) {
                    // find last presented modal
                    viewController = viewController.presentedViewController;
                }

                viewController.presentViewControllerAnimatedCompletion(imagePicker, true, null);
            }
        }
    }
    static recordVideo(onResult, onError) {
        onError("ERROR: For ios this feature is comming soon.");
    }
    static recordAudio(onResult, onError) {
        onError("ERROR: For ios this feature is comming soon.");
    }
    imagePickerControllerDidCancel(picker): void {
        console.log("imagePickerControllerDidCancel")
        this._errorCallback("ERROR: Image capturing cancelled.");
    }
    imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
        console.log("imagePickerControllerDidCancel")
        this._errorCallback("ERROR: Image capturing done.");
    }
}

I'm not getting, what I'm doing wrong and where? Please help me, guys...

2 Answers2

0

I suspect the reason is that your delegate is being cleaned up by garbage collector. One important rule with iOS is, you must always keep a reference of native object in a JS variable to keep it alive.

Try,

private _delegate;

....

this._delegate = Nativemediapicker.new().initWithCallbackAndOptions(onResult, onError, null);
imagePicker.delegate = this._delegate;
Manoj
  • 21,753
  • 3
  • 20
  • 41
0

After adding this line in takePicture function it worked.

imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContext;