How do I detect when the device receives data from the server in iOS


How I can detect when device receive data from server in Objective-C I have 2 problem:

  1. I want to connect to server to receive data through socket. What is library i can use?

  2. I connect to server through socket and i want to detect whenever server push data to device, device listen and receive data? How i can create a thread to listen when server push data? this? I am using Xcode 5.1, iOS 7.x

Updated: I have reading and follow from here: Connect server with socket of raywenderlich

But now I have a question: how keep alive socket during app open and how to detect when app receive data from server?

First you need to decide which socket technology you want to use. Because you/your teammate will be implementing it on the other side. Sockets have one tiny little problem with their awesomness - they tend to be unstable thanks to unstable mobile network. Consider using REST API + APN (Apple Push Notifications).

In case you really really need to stick with sockets, make up your mind which kind you want to use:

Both of them are working great and used widely. The main difference is that Socket.IO has implemented a feature called heartbeat - you always know whether you're connected or not. With TCP socket (especially the framework mentioned above), you don't know that the server is down until you start sending/receiving data (the only way to find out is timeout). Other thing is that Socket.IO is very dynamic framework and is still evolving, yet the framework is very usable. I have implemented both many times, personally I'd go with Socket.IO, but that totally depends on what is implemented on the server side.

Your other question - detect when app receives data:

  • When app is on and opened - both frameworks are made for that quite well
  • When app is in the background - not really, you'll need APN for that. Change the strategy - receive notification from server that something should be downloaded.
  • When app is closed - APN only...