How to avoid GKSession disconnection in client-server when in background - iphone

I've a problem working with GameKit and GKSession operations.
I'm trying to implement a GKSession client-server iOS app system (ServerApp, ClientApp) having this behavior:
ServerApp creates a GKSession in GKSessionModeServer mode (Bluetooth only)
ClientApp detects the server and connects to it
Some data exchange between apps and stuff
Everything is going fine except when ServerApp or ClientApp instance goes to background.
If ServerApp goes to background, Client receives a GKPeerStateUnavailable event and i can't connect to it. What I need is to avoid this event: that means, find a way to keep ServerApp GKSession socket going even when app is in background.
Any help will be appreciated.

Perhaps you could keep the app running in the background by setting the info.plist flag to keep it alive like a music player?
This thread talks about this:
From Raaz

Related

NSStream ends after locking phone

My online app runs on iPhone-3GS iOS 4.3, use NSStream to communicate with server.
When I minimize the app, lock phone, and relaunch my app the streams will end.
So my app loses connection to server.
Anyone knows how to keep the connection after locking phone? Is it a feature of iOS multitask?
This is a feature of iOS. Networking connections of backgrounded apps are cut off. You need to request "VoIP" treatment: for this, you need to set the "voip" value for the Info.plist key UIBackgroundModes and then mark your socket as being a VoIP socket setting the kCFStreamNetworkServiceType of your socket to kCFStreamNetworkServiceTypeVoIP using CFReadStreamSetProperty.
See also the Apple iOS App Programming Guide, section Tips for Developing a VoIP App.

Socket disconnects in iOS5 when lock key pressed

My iPhone application is based on remote desktop protocol which communcates with PC using socket programming. Everything works fine in iOS4, but now i encounter an issue with iOS5 based device.
My socket get's disconnected when user presses lock button in iOS5 based devices. I have developed a cocoa based static socket library which does all communication for my iphone app.
FYI: I just call a function exposed from my library to start the communication. I call it as a background thread using
[self performSelectorInBackGround:#selector(triggerCommunication:) withObject:IP_Address];
a. Why does the socket disconnects on pressing lock button in iOS5 ?
b. Is there a way to prevent the socket from not being disconnect?
Thanks,
If we compile with iOS5 SDK, and run application, then socket disconnects on clicking lock button.
I recompiled my application with iOS4 SDK and tested my application on iOS5 based iPhone. And now socket didnot disconnect even after locking!.
Thanks

GameKit keeping connection going while device screen off or in background

I am working on an appliction that requires bluetooth connectivity, which i use GameKit for data transfer, however im seeing that when i go in background mode, or I just turn off the screen in app, the bluetooth connection is dropped... Ive seen other apps that keep the connection alive in such situations, anyone have any idea if I am missing something that wont cause the connection to drop on such cases? Have been looking around but havent found anything useful...
Thanks
Daniel
This is not intentionally supported by Apple.
If you are writing this for an application that doesn't have to be distributed through the App Store, you can set the application up to play a silent audio file in the background. This will allow GameKit to continue to work even when your application has been put into the background or if the screen has locked.
If this application has to be distributed through the App Store, Apple require the audio to be a real feature, with audible music playing.
If you have two devices connected using GKSession and then one of them is interrupted by call or goes into background, when it wakes up connection should be still alive (you should be able to send/receive packets between devices).
You may try setting
UIRequiresPersistentWiFi
in your plist.

How to properly *connect* background socket at iPhone startup in a VOiP application?

I'm using a VOiP Socket in my iPhone application. I manage to get it working properly, both in background mode, or when the application is in the foreground.
My issue is at iPhone startup: how to be sure that the iPhone has network access (3G or wifi) in order to properly connect the socket without bothering the user ?
Details: a VOiP application is supposed to be started automatically as soon as the iPhone starts : this is working ok, the didFinishLaunching is invoked right after the iPhone startups. But at this time, the user may not have entered his pin code (so that 3G isn't available) and wifi may not be available.
Is there any technique to start automatically the VOiP Socket when network access is ok ?
My current approach that fails : in the didFinishLaunching I keep on trying to start the VOiP socket every 5 secs. If it takes too long without managing to get the connection, the OS is going to kill the app, (max 20s to start), but as the app is flagged as "VOiP", it's going to be started again, and so on...
After a while, once network is OK, the socket is being created , connected and everything seems to works ok, EXCEPT that when data comes to the socket, my callback didReceivedData is not invoked (I display a local notification as soon as I get something from the socket for debug purpose) .
Then, if I start the application just 1 time, and then quit it (home button), so that the application is put in the background, in that case, the socket callback is properly invoked and I see local notifications being displayed, proof that the socket is properly waken up in that case.
I would like to get the VOiP socket up and running right from iPhone startup (once network is OK) without having the user to launch the application 1 time. Any idea about how to achieve this ?
Use the Reachability class to poll for connectivity every X seconds, THEN try to connect.
Reachability won't take as long as creating the VoIP socket will.

Can I discover other iOS devices over Bluetooth while in the background?

I would like to be able to discover other iOS devices over Bluetooth while my application is in the background. Is it possible to use Bonjour or Game Kit to do this discovery while my application is not in the foreground?
Would it be possible to do this and fire off a notification if a compatible device is discovered?
Also, would I be able to run in the background while playing audio and do this detection?
This is not possible while your application is suspended. From the iOS Application Programming Guide:
Cancel any Bonjour-related services before being suspended. When your
application moves to the background,
and before it is suspended, it should
unregister from Bonjour and close
listening sockets associated with any
network services. A suspended
application cannot respond to incoming
service requests anyway. Closing out
those services prevents them from
appearing to be available when they
actually are not. If you do not close
out Bonjour services yourself, the
system closes out those services
automatically when your application is
suspended.
Be prepared to handle connection failures in your network-based
sockets. The system may tear down
socket connections while your
application is suspended for any
number of reasons. As long as your
socket-based code is prepared for
other types of network failures, such
as a lost signal or network
transition, this should not lead to
any unusual problems. When your
application resumes, if it encounters
a failure upon using a socket, simply
reestablish the connection.
However, if your application is streaming audio, it would be necessary for it to maintain network connections, so you should be able to do Bonjour discovery while in the background for an application continuously playing audio. Make sure you don't abuse this by playing a silent audio clip in a loop just so that you can stay in the background, or your application will be rejected.