tags:

views:

1376

answers:

2

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?

+2  A: 

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.

Alex Reynolds
Thanks for your reply. I think myApp.idleTimerDisabled = YES; will not be a feasible solution though. I was wondering about UIRequiresPersistentWifi option. It should work. Do I need to include the info.plist file somewhere in my project?
ebaccount
I wanted to add one more point, I am sending periodic keepalives to the server. However I am using socket () (not CFStream)
ebaccount
I think you may need the idleTimedDisabled flag set to YES. UIRequiresPersistentWifi just makes sure that the WiFi radio is turned on, but that doesn't prevent your iPhone from going to sleep.
Alex Reynolds
A: 

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.

Rob Napier
Thanks for your reply. By "logs me out" I mean that the application looses its TCP connection with the server.
ebaccount
This is what I mean by "there are only packets." The act of me turning off my computer does not break a TCP connection. And TCP does not have any way to know if the other side is still around. The only things that terminate a TCP session are FIN, RST, and failure to respond when the other side talks to me. "TCP keepalive" is misleading here because it doesn't even come into play for a couple of hours. So, why do you think you've lost your TCP connection? Is the server sending you data that you're not responding to?
Rob Napier
Yes, after a while I am not receiving data from the server
ebaccount
This doesn't mean that the TCP session has closed. First, what is "after a while?" Is it when you go idle (you can't receive data while you're idle)? Or is it after about 30 minutes (in which case it's your wifi turning off)? If you want to receive data all the time, you can't let the device go idle. There's no work around for that; a computer cannot both do work and be asleep. If you control the server, then you should improve your protocol to manage devices that want to go away for awhile.
Rob Napier
Also, even if you don't control the protocol, you have access to -applicationWillResignActive:, during which you can cleanly close your connection to the server, and -applicationDidBecomeActive: during which you can reconnect.
Rob Napier
This is actually a chat application. Both A, and B connects to the server and starts chatting. Then say B goes to sleep after 1 min. A keeps typing and B receives the msgs from A (I know because whenever the app recvs a msg I play a sound). Now, right after 3 mins B stops receiving data from A. Could you pls let me know what could be the problem. I am using socket() APIs.
ebaccount
You're almost certainly going from screen lock to sleep. The times are right. So when you go to screen lock you should tell the server you're away. If you want to stay online, then you will need to turn off the idle timer when you want to be protected from sleep.
Rob Napier
Hi again. Thanks for your reply. I think if I set idleTimerDisabled=YES, then the screen will never go dim and will never turn off, right? Therefore, it will consume more battery power. However, I have seen applications like yahoo messenger for iPhone, even if it goes to sleep (the screen is dark), it remains logged in to the service and can receive data even after 3 mins. Could you please comment on that?
ebaccount
Are you certain that these applications are receiving data, versus simply requesting the data they missed when they wake up? There are other work arounds (such as playing silent audio), but they are not recommended. See this thread for more information from Apple: https://devforums.apple.com/message/23294#23294. Also the Guide: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/ApplicationEnvironment.html
Rob Napier
Here is the update, I am using [[UIApplication sharedApplication] setIdleTimerDisabled:YES];. The result is the screen never goes to dim and the screen never turns off. Is there any way that it won't go to sleep but the screen will go off to save battery? I saw this in the above mentioned link"And BTW also cool is to have the screen be switched off but keep your app working even if you put the iphone in your pocket. This saves battery and prevents accidental pushes: [[UIApplication sharedApplication] setProximitySensingEnabled:YES];"Could you please comment on that?
ebaccount
If you turn on proximity sensing, then the screen turns off and user interaction is disabled when there is something large (generally the user's face) near the screen. This is what turns off the screen when you're talking on the phone. It used to be private, but Apple declared it public after Google started using it. If you're running code (to get or send packets) and the wifi, then you're draining standby time. I don't know of anyway to actively turn off the screen. You can set the window's view to a black view, but you should actually test that to see what the difference is in battery life.
Rob Napier
Hi, One more thing. I am now using Fring, an application that allows Yhaoo/MSN etc service to use. I have seen that the screen is turned off but it never looses its network connection after the idle. This is evident from the fact that I always see the user on iphone always online in the buddy list of another user on PC. Could you please comment on that?
ebaccount
and I can send the user on Iphone any text msgs any time and it is received instantly (it plays a sound when it receives it)
ebaccount
so the question now becomes, how can I keep network connection alive and turn off the screen at the same time (when it is idle)?
ebaccount
to rephrase the requirement, the iPhone will turn off its screen, but still I will have network connection alive.
ebaccount
I think playing a silent audio is Ok in this case to keep the device away from going to sleep and it will also let the screen goes off. Now could you please let me know how can I do that (playing a background sound)?
ebaccount
http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
Rob Napier
Thanks again. I am getting a compiler error, it cannot find AVAudioPlayer.h
ebaccount
fixed by adding #import <AVFoundation/AVFoundation.h>
ebaccount
now I am getting linker error
ebaccount
ok I have fixed the linker error by adding the framework from this path: Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/System/Library/Frameworks/
ebaccount
but here is the interesting thing, when the iPhone goes to back screen, it stops playing the sound as well. when I wake it up, it starts playing again. But I want the sound to be played all the time. Could you let me know how can I do that?
ebaccount
* back= black screen (the screen is turned off)
ebaccount
Fixed and everything is working just as I expected. Thanks a lot.
ebaccount