Is there anything like 'getStreamsToHost' on real iPhone device?

I want to write an NSOutputStream to a server with apple’s sample code:


NSURL *website = [NSURL URLWithString:str_IP];
NSHost *host = [NSHost hostWithName:[website host]];
[NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];

These codes works well on the iPhone simulator, but when I build it to real device. Two warnings pop up. The problem is:

  • Method Syntax in Objective C
  • iOS 8 Suggested Apps - How does it work?
  • Get current view controller from the app delegate (modal is possible)
  • KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?
  • Calling a function from a different view controller for iphone
  • AutoLayout row height miscalculating for NSAttributedString
  • 1)class NSHost doesn’t belong to iphone os library

    2)getStreamsToHost is not found either

    Any suggestions for the alternative method or class which can be used on real device?

    Solutions Collect From Internet About “Is there anything like 'getStreamsToHost' on real iPhone device?”

    Since CFWriteStream is toll-free bridged to NSOutputStream you can use CFStreamCreatePairWithSocketToHost to get your stream pair:

    CFReadStreamRef readStream = NULL;
    CFWriteStreamRef writeStream = NULL;
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)host, port, &readStream, &writeStream);
    if (readStream && writeStream) {
        CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
        CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    
        inputStream = (NSInputStream *)readStream;
        [inputStream retain];
        [inputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];
    
        outputStream = (NSOutputStream *)writeStream;
        [outputStream retain];
        [outputStream setDelegate:self];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream open];
    }
    
    if (readStream)
        CFRelease(readStream);
    
    if (writeStream)
        CFRelease(writeStream);