billy_comic
billy_comic

Reputation: 899

Can't Get Error To Return To Ionic/Cordova App From Objective C Plugin

I have written a plugin for a bluetooth zebra printer. I can make it work and it prints fine, but I am needing to return an error back to my app if the printer is either off or not connected. I'm able to get this to open in an alert directly from Objective C, but I really need to return the error back to my mobile app so I can create new actions for the user if it does error out.

The code compiles and builds, but when I run the code it does not return the error back to my javascript error function. Keep in mind I'm not very familiar with Objective C and am working my way through this.

Here is the Objective C code that should be sent back to the app (I can see that the code steps into this function, but doesn't get sent back) :

__block CDVPluginResult* result; //Declared at the beginning of the print function in .m file

dispatch_async(dispatch_get_main_queue(), ^{
            if(success != YES || error != nil) {

result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                          messageAsString:[NSString stringWithFormat:
                          @"Either the printer is turned off or not connected "]];
            }
        });

Here is my javascript function where I initiate the call to the printer.

$scope.printTicket = function () {
 $ionicPlatform.ready(function () {

    cordova.plugins.zebra.printer.sendZplOverBluetooth($scope.printObj, function successCallback () {
      console.log('SUCCESS: Print');

    }, function errorCallback () {
      console.log('ERROR: Print');

    });

 })
}

Any help greatly appreciated.

Upvotes: 0

Views: 212

Answers (1)

DaveAlden
DaveAlden

Reputation: 30356

You need to actually send the plugin result after creating it with sendPluginResult - something like:

- (void) yourPluginFunction: (CDVInvokedUrlCommand*)command
{
    __block CDVPluginResult* result; //Declared at the beginning of the print function in .m file

    dispatch_async(dispatch_get_main_queue(), ^{
        if(success != YES || error != nil) {

            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                messageAsString:[NSString stringWithFormat:
                @"Either the printer is turned off or not connected "]];

            [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
        }
    });
}

Upvotes: 1

Related Questions