tags:

views:

1056

answers:

5

Hi,

My application needs Internet Connection. It seems like if I keep my iPhone idle for a while it shuts down its 3G connection. Once I wake it up (slide to unlock) and run my application, it cannot connect to the Internet. I have to run Youtube or Safari first, then it gets the Internet connection, then I have to shut down Youtube/ Safari and then I can use my application to login to my service.

Could you please let me know how can I activate 3G connection from my application (so that I can use my application directly after it wakes up from the idle state and I do not have to run other applications like Youtube/ Safari?

Thanks.

+1  A: 

There is obviously another better solution, but you could load a blank page with:

[NSString stringWithContentsOfUrl ... ]

The connection will be established if it is necessary.

Rémy BOURGOIN
Could you pls let us know the API?
ebaccount
@raihan - what do you mean by API here? simply execute remy's code : [NSString stringWithContentsOfUrl...] to access the internet again
Raj
+2  A: 

To disable the idle timer, take a look at the idleTimerDisabled property of the UIApplication class.

From Apple:

The default value of this property is NO. When most applications have no touches as user input for a short period, the system puts the device into a “sleep” state where the screen dims. This is done for the purposes of conserving power. However, applications that don't have user input except for the accelerometer—games, for instance—can, by setting this property to YES, disable the “idle timer” to avert system sleep.

Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.

Martin Gordon
Thanks for your reply Martin. This does not solve the original problem . I still have to start youtube/safari type application that will enable 3G connection and then I can run my application. Moreover, this method will consume iPhone power as well.
ebaccount
What are you using to connect to your application? I make calls to an external site when my application starts up and it accesses the internet right away.
Martin Gordon
I am using socket functions like connect(...) which is not enabling the 3G connection. Could you pls let me know what API you are using to make calls to your external site? I think that API is activating the 3G connection.
ebaccount
I'm using the Cocoa NSURLRequest and NSURLConnection APIs. Like Remy says below, though, you could simply load a string with a blank/small page.
Martin Gordon
Hello Martin,Yes, this worked for me. I have added more comments in response to "rpetrich" below.
ebaccount
A: 

Are you sure you're establising the connection correctly? My application does the same using sockets and it has no problems to re-establish the connection after device sleep. Use Reachability API in SystemConfiguration framework to get notified when coverage is available and after that make your connection attempt. Note that a time period - from several seconds to couple of minutes - has to elapse after the device awakes to gain Internet connectivity, so be patient.

There is Reachability sample from Apple, search also stackoverflow for reachability and you'll find more hints how to implement it.

Vladimir Grigorov
A: 

Only NSURLConnection (and any APIs that are layered on top of it) reinitializes the data connection after waking from sleep. To reinitialize the data connection create a dummy NSURLConnection to a non-local address and cancel it right away; then the socket API will work as expected.

There is a post on the developer forums where an Apple dev explains this in detail (but I can't find it at the moment)

rpetrich
Yes, this worked for me. now could you let me know what do you mean by non-local address and how can I cancel the NSURLConnection right away? I do not have to send the request to retrieve the dummy page (say google.ca)?
ebaccount
Local addresses are in one of the following ranges: 10.x.x.x, 169.254.x.x, 172.16.x.x through 172.31.x.x and 192.168.x.x; or any address that resolves to an IP in that range (DNS requests are cached, so you cannot guarantee that they will wake the data connection).To cancel, send the -cancel message to a connection that has been initialized with +[NSURLConnection connectionWithRequest:delegate:] (or alloc, initWith..., start and then cancel separately)
rpetrich
A: 

Actually, you get the same problem when you change the network settings on your phone between launches of the application. For instance let's say that you use the WIFI connection when you launch the app. Then you close the app and switch off the WIFI so that the device uses the carrier's network. When you relaunch the app the socket won't be able to connect unless you do the trick with the dummy NSURLConnection (or you launch the browser before lanuching the app).

Also, canceling the NSURLConnection right after initializing it (with connectionWithRequest or initWithRequest) did not work for me. Either do not cancel the request or wait some time before canceling it (e.g. with performSelector:withObject:afterDelay:).

Marc