To close NSStream objects before resigning active - ios

I have an NSInputStream and NSOutputStream scheduled in a non-main-thread run loop. I'm using CFStreamCreatePairWithSocketToHost. The remote endpoint is an external device that communicates with the app with TCP over wifi.
When applicationWillResignActive is called, I want to close these. I want to be guaranteed that they will close, because another app may want to communicate with the same device, and the device only accepts one TCP connection.
Additional information:
It is a frequent occurrence in the context of this device that multiple apps may need to communicate with the device.
I have tried using performSelector:onThread:withObject:waitUntilDone:
(with waitUntilDone:YES) in applicationWillResignActive. Sometimes this doesn't work, and in those cases it seems that the selector does not get called.
My understanding is that I can't close the NSStream objects or destroy the task queue from the main thread.
I can not change how the device receives connections. It listens on one address/port for TCP connections, and allows only one connection (it doesn't multiplex connections by source port).

Use dispatch_sync() to the background queue.

Related

iOS Backgrounding mode Location Updates - but also keep a socket open

I have a navigation application that works with both CoreLocation (Backgrounding mode Location updates) as well as GPS data provided over a UDP connection from an external sensor.
I've noticed that when I background the app the socket goes down (which likely makes sense).
What is the procedure to keep the socket open in backgorunding?
Idea
I thought perhaps to register as a VoIP app - would keep the socket open but it looks like that works differently now.
The documentation suggest implementing setKeepAliveTimeout:handler: but this appears to be deprecated.
I'm not sure exactly how to proceed. Any ideas? The best I can come up with is a hack to have my location-update handler run a check on the socket so see if there is new data - but I'm assume there is a legitimate way to do this.
When the phone goes in stand-by all the UDP socket are closed and only TCP connection can be used. Yes probably with VOIP app you can use the UDP but in that case your app will be reject because your don't use a real VOIP service. I had the same problem ... In my case, even if the phone is in background, I want to send UDP message to a domotic system but is not possible.

Sync TCP Client in multi-client application without lags

I have application, whitch connents to N devices trought the network.
The problem is:
TCP Client class isn't async, I'm send the command to device, I want get answer now.
So, 2 or more active clients makes my app frozen.
Any ideas how to do some, that's will not freeze my application if many devices is enabled at one time?
TCP Client class uses Synapse classes package.
Thanks!
To avoid application hang, use a separate thread for every TCP client connection.
The thread then will run independently, connect to the device, and do the communication.
Note that the thread must not access objects/data in the main application thread without proper synchronization.

Is using a socket connection possible within iOS 7 background fetch mode?

Apple added in iOS 7 new possibility to execute activities in the background, fetch mode, which can be used e.g. for Twitter, when frequently small amounts of data need to be downloaded (official description: The app regularly downloads and processes small amounts of content from the network.),Apple Document Reference
However, I need to use a TCP connection.
Does anyone know whenever TCP connection may be used in such scenario between the calls?
The scenario is:
I open the TCP socket in the beginning and then the application can use it in these fetch calls. Of course handlers are needed when the TCP connection drops, but there is a difference between rare necessity to reconnect and constant (i.e. in each fetch iteration) need to reconnect.

How to prevent Web Socket from disconnecting while iPhone/iPad goes in lock state?

We have implemented HTML5 Web socket in my iPhone and iPad app. Web Socket works fine when app goes background (iPad/iPhone is not in lock state). But it gets disconnect when iPhone or iPad gets lock.
Is there any way to keep web socket running even when iPhone/iPad is lock?
Thanks.
According to my own experiments. When you lock the device, the device seems to shut down WiFi and close network sockets, most likely in order to save power. The mobile broadband connections remains active though, and sockets seem to survive. At least for some time.
Have you tried to wrap the some parts of the socket code inside a beginBackgroundTaskWithExpirationHandler()? This should at least allow you to gracefully close the socket, or keep the socket alive for some time. The OS will, however, kill your task it after a global timer expires.

socket connection in iOS and background tasks

I am designing an app that needs to exchange data in real time over network and I am planning to use socket connection to achieve this. To keep the socket connection open in background I plan to declare my app as a VOIP app (I already know apple's rule prohibiting this and willing to take the risk), and use keepAliveTimeout to keep connections alive.
Now can I declare and own a socket which will listen to any incoming data or I have to use an existing/specific socket given by Apple? Also is the port unique per app or per phone? Means if my app is continuing in background and also a call comes over Skype would they interfere with each other?
Thanks in advance for response. Help would be appreciated.

Resources