ios - 在委托(delegate)方法返回时使用 Cocoa 异步库的 Phonegap 插件中缺少回调 ID

标签 ios cocoa cordova callback

我无法让我的插件在使用 Cocoa 异步套接字库的 Phonegap 3.3 上正常运行。问题出现在下面代码中的 didReadData 方法中。当我将响应数据输出到控制台时,响应数据是正确的,但是,我无法使 javascript 代码的回调正常工作。我能说的最好的是 [self writeJavascript:[pluginResult toErrorCallbackString:callbackId]] 中使用的 callbackId 与 sendMessage 方法中计算的 callbackId 不同。非常感谢任何帮助!

/********* TCPSockets.m Cordova Plugin Implementation *******/

#import "TCPSockets.h"
#import "GCDAsyncSocket.h"

@implementation TCPSockets

#define ENABLE_BACKGROUNDING  1

#pragma mark Plugin Start

- (void)sendMessage:(CDVInvokedUrlCommand*)command;
{
NSString* sendMessage = [command.arguments objectAtIndex:0];
NSString* myCallbackId = command.callbackId;

NSLog(@"Callback Id is %@", myCallbackId);

if (sendMessage == nil) {
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                               messageAsString: @"Error: No parameters for plugin"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
}
else
{
    port = [[command.arguments objectAtIndex:0] retain];
    host = [[command.arguments objectAtIndex:1] retain];
    message = [[command.arguments objectAtIndex:2] retain];
    connectionTimeout = [[command.arguments objectAtIndex:3] retain];
    secureConnection = [[command.arguments objectAtIndex:4] retain];
    response = @"";
    error = nil;

    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

    if ([secureConnection boolValue])
    {
        NSLog(@"Connecting (SSL) to \"%@\" on port %i...", host, [port intValue]);

        if (![asyncSocket connectToHost:host onPort:[port intValue] withTimeout:    ([connectionTimeout intValue] / 1000) error:&error])
        {
            NSLog(@"Error connecting: %@", error);

            response = error.localizedDescription;
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];

        }
    }
    else
    {   
        NSLog(@"Connecting to \"%@\" on port %i...", host, [port intValue]);

        if (![asyncSocket connectToHost:host onPort:[port intValue] withTimeout:([connectionTimeout intValue] / 1000) error:&error])
        {
            NSLog(@"Error connecting: %@", error);

            response = error.localizedDescription;
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
            [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
        }
    }
}
}



- (id)init
{
if ((self = [super init]))
{
    // Setup logging framework
    //[DDLog addLogger:[DDTTYLogger sharedInstance]];
}

return self;
}

#pragma mark Socket Delegate

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, self->host, (UInt16)self->port);

if ([secureConnection boolValue])
{
    // SSL connection

    #if ENABLE_BACKGROUNDING && !TARGET_IPHONE_SIMULATOR
    {
        // Backgrounding doesn't seem to be supported on the simulator yet

        [sock performBlock:^{
            if ([sock enableBackgroundingOnSocket])
            {
                NSLog(@"Enabled backgrounding on socket");
            }
            else
            {
                NSLog(@"Enabling backgrounding failed!");
            }
        }];
    }
    #endif

    // Configure SSL/TLS settings (see documentation for the startTLS method in GCDAsyncSocket.h)
    NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:3];

    // To connect to a test server, with a self-signed certificate, use settings similar to this:

    // Allow expired certificates
    [settings setObject:[NSNumber numberWithBool:YES]
                 forKey:(NSString *)kCFStreamSSLAllowsExpiredCertificates];

    // Allow self-signed certificates
    [settings setObject:[NSNumber numberWithBool:YES]
                 forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];

    // Don't validate the certificate chain
    [settings setObject:[NSNumber numberWithBool:NO]
                 forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];

    NSLog(@"Starting SSL with settings:\n%@", settings);

    [sock startTLS:settings];
}
else
{

    #if ENABLE_BACKGROUNDING && !TARGET_IPHONE_SIMULATOR
    {
        // Backgrounding doesn't seem to be supported on the simulator yet

        [sock performBlock:^{
            if ([sock enableBackgroundingOnSocket])
            {
                NSLog(@"Enabled backgrounding on socket");
            }
            else
            {
                NSLog(@"Enabling backgrounding failed!");
            }
        }];
    }
    #endif

    NSData *requestData = [[message dataUsingEncoding:NSUTF8StringEncoding] retain];

    NSLog(@"Sending:%@", message);

    [sock writeData:requestData withTimeout:([connectionTimeout intValue] / 1000) tag:0];
    [sock readDataToData:[GCDAsyncSocket LFData] withTimeout:([connectionTimeout intValue] / 1000) tag:0];
}
}

- (void)socketDidSecure:(GCDAsyncSocket *)sock
{
NSLog(@"socketDidSecure:%p", sock);

NSData *requestData = [[message dataUsingEncoding:NSUTF8StringEncoding] retain];

NSLog(@"Sending:%@", message);

[sock writeData:requestData withTimeout:([connectionTimeout intValue] / 1000) tag:0];
[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:([connectionTimeout intValue] / 1000) tag:0];
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"socket:%p didWriteDataWithTag:%ld", sock, tag);
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{

NSLog(@"socket:%p didReadData:WithTag:%ld", sock, tag);

response = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] retain];

response = [[response stringByReplacingOccurrencesOfString:@"\n" withString:@""] retain];

NSLog(@"Resposne data: %@", response);

pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:response] retain];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];


// Release instance of GCDAsyncSocket
[asyncSocket setDelegate:nil delegateQueue:NULL];
[asyncSocket disconnect];
[asyncSocket release];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSLog(@"socketDidDisconnect:%p withError: %@", sock, err);

if (err != nil) {
    response = [err.localizedDescription retain];
    pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:response] retain];

    [self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
} else {
    response = [[response stringByReplacingOccurrencesOfString:@"\n" withString:@""] retain];
    pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:response] retain];

    [self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
}

@end

最佳答案

在你的 .h 上创建一个属性

@property (nonatomic, strong) NSString* myCallbackId;

在 sendData 上改变

NSString* myCallbackId = command.callbackId;

self.myCallbackId = command.callbackId;

在 didReadData 上改变

[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];

[self writeJavascript:[pluginResult toErrorCallbackString:self.callbackId]];

关于ios - 在委托(delegate)方法返回时使用 Cocoa 异步库的 Phonegap 插件中缺少回调 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615195/

相关文章:

ios - 从一点到另一点的弯曲箭头

xcode - Cocoa - 如何检测无处不在的容器中的变化

macos - 是否可以为付费 Mac 应用程序提供促销代码(无应用内购买)

android - Phonegap Android 内联 SVG

java - Cordova 找不到 $ANDROID_HOME

cordova - cordova 插件文件夹中的 fetch.json 文件的用途是什么?

ios - 可以通过 BLE iOS 传输文件(~1MB)吗?

ios - 按钮可以包含 Apple Watch 应用程序中的组吗?

ios - 从 Swift 中的非静态方法访问静态变量

cocoa - IKImageBrowserView 中的就地标题编辑