I’m currently trying to connect to a tcp server. Everything works really good, but if the server is not available, the app freeze about 30 seconds and that’s not really nice. I’ve searched for a resolution, but couldn’t find something usefull so far.
Maybe someone of you have got a solution to check a connection?
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)@"IP_SERVER_PATH", 6666, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
Have you tried using something like Reachability?
There is a sample project by Apple, but there are many updates of it on Github such as this one that works with ARC and GCD.
Basically, you monitor your network connection and handle events gracefully when it drops off.