Monday, 7 December 2020

Show native IOS button from cordova plugin

I am trying to write a cordova plugin that will add an iOS native button (UIButton I think?) to the cordova view. I am a complete amateur at this stuff so I would appreciate some help. This is my plugin code:

#include "CordovaButton.h"

@implementation CordovaButton

- (void)pluginInitialize
{

}

- (void)getButton:(CDVInvokedUrlCommand *)command
{

    // define error variable
    NSString *error = nil;

    // create array to hold args
    NSArray* args = command.arguments;

    // create variable to hold response
    CDVPluginResult* pluginResult;

    // map command inputs
    NSString* callbackUrl = [args objectAtIndex:0];
    NSString* useCaseId = [args objectAtIndex:1];
    NSString* clientSdkId = [args objectAtIndex:2];
    NSString* scenarioId = [args objectAtIndex:3];

    if ([callbackUrl length] == 0) {
        error = @"missing callbackUrl";
    }

    if ([useCaseId length] == 0) {
        error = @"missing useCaseId";
    }

    if ([clientSdkId length] == 0) {
        error = @"missing clientSdkId";
    }

    if ([scenarioId length] == 0) {
        error = @"missing clientSdkId";
    }

    if ([error length] > 0) {
        pluginResult = [
            CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Missing an input"
        ];
    } else {
        pluginResult = [
            CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"it works"
        ];
    }
        
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 230, 48)];
    [button setTitle:@"my button" forState:UIControlStateNormal];
    
    [self.viewController.view addSubview:button];
}

@end

This getButton method is being called from the javascript side and I would be expecting a native button to appear on the screen, but nothing is happening. The debug console in xcode just says ERROR: {} which isn't really helpful. What is going wrong?



from Show native IOS button from cordova plugin

No comments:

Post a Comment