javascript - 无法从 Objective C 插件返回 Ionic/Cordova 应用程序

标签 javascript ios objective-c cordova ionic-framework

我为蓝牙斑马打印机编写了一个插件。我可以让它工作并且打印得很好,但是如果打印机关闭或未连接,我需要将错误返回到我的应用程序。我可以直接从 Objective C 在警报中打开它,但我确实需要将错误返回到我的移动应用程序,以便在出现错误时我可以为用户创建新操作。

代码可以编译并构建,但是当我运行代码时,它不会将错误返回到我的 javascript 错误函数。请记住,我对 Objective C 不太熟悉,并且正在努力解决这个问题。

这是应该发送回应用程序的 Objective C 代码(我可以看到代码进入此函数,但不会被发送回):

__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 "]];
            }
        });

这是我的 JavaScript 函数,我在其中发起对打印机的调用。

$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');

    });

 })
}

非常感谢任何帮助。

最佳答案

您需要在使用 sendPluginResult 创建插件结果后实际发送它 - 类似于:

- (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];
        }
    });
}

关于javascript - 无法从 Objective C 插件返回 Ionic/Cordova 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41746852/

相关文章:

javascript - 为什么警报 ("1"- - "1");在 javascript 中生成 2?

objective-c - Interface Builder 中的圆形标签...?

ios - CoreData一对多关系问题

javascript - jquery UI 模式不会导致回发

javascript - 带有图像 slider 的 Div 仅在 Safari 中不继承高度

android - Flutter 多平台 Android/iOS 抽屉式菜单

ios - 代码 : How To Customize Text Field Font With My Own TTF Font?

objective-c - 基本 IOS OOP - 结构和实现

iphone - NSTask实时监控输出

javascript - 如何在jquery中使用图像id获取图像位置