Reputation: 21
I'm working on an iOS app which needs to maintain a connection to a web service.
I thought about developing a RESTFul
protocol between my app and the server.
But the point is that the server must also be able to push data to the app.
So I'm wondering if I can accomplish that only with the apple sdk by using the NSURLConnection
and the xml parser provided...
To be more precise, is it possible to maintain a NSURLConnection
for as long as the app is running and process data coming from the server without having to request it from the app ?
Do I have to send Keep-alive packets or something ?
Thanks in advance for your help !
Upvotes: 2
Views: 322
Reputation: 1137
One solution would be to have a RESTful interface between the application and use Apple Push notifications. Then iOS takes care of the underlying connection to the push server. In fact it keeps a TCP connection alive. All the communication to the server could use your RESTful services, including the registration of tokens that you need to send to your own server. This is a solution I have used myself and it works like a charm.
Upvotes: 0
Reputation: 31304
You will need to send keep-alive packets or similar, because your connection will at some point have a time-out (you could of course just set a very, very high timeout for your NSURLConnection
, but that's not the best idea as you would find it hard to know when your service had genuinely timed out).
That said, it is problematic to do what you're proposing on mobile, because powering the cellular radio takes up a comparatively huge amount of battery. Apple's documentation strongly recommends you keep network traffic to a minimum. The best solution is probably going to be doing what Apple do for push notifications - using a sockets to push your data over. This is quite a bit more complex than just using NSURLConnections
, however.
Upvotes: 1