84105

NSStream on iphone not working

Question:

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'

Answer1:

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

Recommend

  • How to test async method in block using OCMock
  • How to use NSInputStream and NSOutputStream
  • Count while in background (NSTimer for more than 3 mins)
  • In Swift3, KVO the alpha of a UIView?
  • NSRunLoop freezes with NSTimer and any input
  • upload files in background via ftp on iphone
  • Swift cannot output when using NSTimer
  • Does Apportable support to build library binary (.a/.so)?
  • Getting media player state in windows phone 7
  • Lock Horizontal View
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • jQuery Orbit - How to make a Random Slideshow?
  • Debugging VB6 Code From Visual Studio 2010
  • Build Successful but not running on simulator
  • Button text different than value submitted in query string
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • iOS: Detect app start via notification press
  • Bad request using file_get_contents for PUT request in PHP
  • Meteor helpers not available in Angular template
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Is calc() supported in html email?
  • req.body is undefined - nodejs
  • swift auto completion not working in Xcode6-Beta
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Acquiring multiple attributes from .xml file in c#
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?