0

我遇到了一个问题。相同的代码在 iPhone (iOS 5) 和 iPhone/iPad 模拟器上运行良好。但它不适用于 iPad (iOS 5)。我会很感激任何帮助。

这是读取端口代码:

//Code for read port.
 CFIndex bytesRead = CFReadStreamRead(inputStream, bufferPoint, 1024);
 if (bytesRead < 0) {
     NSLog(@"bytesRead < 0");
     CFErrorRef error = CFReadStreamCopyError(inputStream);
     //reportError(error);
     DEBUGLOG(@"readResponse error \n")

在上面之前,有连接部分代码。

//prevent to release before relocate
if ((inputStream != nil) && (outputStream != nil)) {
    [inputStream release];
    inputStream = nil;
    [outputStream release];
    outputStream = nil;
}
[NSStream getStreamsToHostNamed:relayHost port:relayPort inputStream:&inputStream outputStream:&outputStream];
//[self lgetStreamsToHostNamed:relayHost port:relayPort inputStream:&inputStream outputStream:&outputStream];
if ((inputStream != nil) && (outputStream != nil))
{
    sendState = kIMAPConnecting;

    isSecure = NO;

    [inputStream retain];
    [outputStream retain];

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];


    result = [inputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
    DEBUGLOG(@"inputStream setProperty result: %d", result);
    result =[ outputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
    DEBUGLOG(@"outputStream setProperty result: %d", result);


    if (!CFReadStreamOpen(inputStream)) {
        DEBUGLOG(@"inputStream open failed");
        return NO;
    }

    if (!CFWriteStreamOpen(outputStream)) {
        DEBUGLOG(@"outputStream open failed");
        return NO;
    }



    self.inputString = [NSMutableString string];        

    DEBUGLOG(@"SCRIMAPMessage startToConnect end with YES\n");
    return YES;
}
4

1 回答 1

0

以下内容无法通过 iOS 获得:

[NSStream getStreamsToHostNamed:relayHost port:relayPort inputStream:&inputStream outputStream:&outputStream];

我真的不知道它如何在 iPhone 的 iOS 上运行。

你的选择很简单...

A) 如 Apple 的技术说明中所述,在 NSStream 上创建一个类别:here

B) 使用 CFStreamCreatePairWithSocketToHost() 并简单地桥接 CFReadStreamRef/CFWriteStreamRef

我推荐(B),因为它将为您提供灵活性的最佳选择。更具体地说,您可以创建自己的 StreamObject 类来处理此问题,并将流委托合二为一。

快乐编码!

于 2013-03-02T18:02:04.393 回答