NSStream on iphone not working

空扰寡人 提交于 2019-12-08 07:04:47

问题


  func connect(host: String, port: Int)
{

    self.host = host
    self.port = port

    NSStream.getStreamsToHostWithName(host, port: port, inputStream: &inputStream, outputStream: &outputStream)

    if inputStream != nil && outputStream != nil
    {
        // Set delegate
        inputStream!.delegate = self
        outputStream!.delegate = self

        // Schedule
        inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
        outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)

        // Open!
        inputStream!.open()
        outputStream!.open()
    }
}

When I start above code in the XCode Simulator it works, but it gives me the following error when I start it on my iPhone:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSStream getStreamsToHostWithName:port:inputStream:outputStream:]: unrecognized selector sent to class 0x3b81786c'


回答1:


getStreamsToHostWithName(_:port:inputStream:outputStream:) in NSStream is not avaialable in iOS 7. It's introduced in iOS 8.

extension NSStream {

    @availability(iOS, introduced=8.0)
    class func getStreamsToHostWithName(hostname: String, port: Int, inputStream: AutoreleasingUnsafeMutablePointer<NSInputStream?>, outputStream: AutoreleasingUnsafeMutablePointer<NSOutputStream?>)
}

You have to use CFStreamCreatePairWithSocketToHost instead:

class Connection: NSObject, NSStreamDelegate {

    var inputStream:NSInputStream?
    var outputStream:NSOutputStream?

    func connect(host: String, port: Int) {

        var inStreamUnmanaged:Unmanaged<CFReadStream>?
        var outStreamUnmanaged:Unmanaged<CFWriteStream>?
        CFStreamCreatePairWithSocketToHost(nil, host, UInt32(port), &inStreamUnmanaged, &outStreamUnmanaged)
        inputStream = inStreamUnmanaged?.takeRetainedValue()
        outputStream = outStreamUnmanaged?.takeRetainedValue()

        if inputStream != nil && outputStream != nil {
            // Set delegate
            inputStream!.delegate = self
            outputStream!.delegate = self

            // Schedule
            inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
            outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)

            // Open!
            inputStream!.open()
            outputStream!.open()
        }
    }


来源:https://stackoverflow.com/questions/28971858/nsstream-on-iphone-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!