Tutorial :Losing network connectivity on iPhone



Question:

I am developing a network application on iPhone that requires internet connection all the time. However, once I login to the server and keep the iPhone idle for a while, the iPhone goes to sleep mode and disconnects my network connection (it logs me out).

If I run the same application on iPhone, while the iPhone is connected to the PC through USB cable, it never loses its network connection.

In the info.plist file I have added these two flags, but does not seem to have any effect.

UIRequiresPersistentWifi -> true SBUsesNetwork - integer ->3

Am I missing anything? Could you please let me know how can I make sure that the network connection is persistent throughout the life of the application?


Solution:1

What do you mean by "logs me out" here? At the network level, there is no "logged in" (*). There are only packets. You send them or you don't. So does your server process have some expectation of packets or messages arriving periodically? If it does, then you must send them, and that means that you can't go idle (idleTimerDisabled = YES). If you control the server, it is better to make it less demanding about how often you talk to it. This all happens well above the network layer, however.

UIRequiresPersistentWifi means that the Wifi radio is kept on while you're app is running, even if you don't talk on it. This is important for receiving data. Otherwise you drop off the network and others can't talk to you after about 30 minutes. It should be set in Info.plist, but this is certainly in your app bundle. If it weren't, your app wouldn't launch, so that isn't the problem.

(*) The cell network does have the concept of logged in, but that's not what's causing your problem.


Solution:2

In your application delegate ("appDelegate"), disable the idle timer in the +initialize method:

myApp.idleTimerDisabled = YES;  

Note that this will keep your iPhone from sleeping while your app is open. This can present issues with battery life.

Another option might be to set up a background thread that opens a small CFStream on a timed basis.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »