0

In iOS 5 When Application Enter background wi-fi connection is lost.

But I want to use wi-fi connection for the next 4-5 minutes before the device sleeps as some tasks can be performed within 4-5 minutes of application enter background.

I think this can be accomplished by using beginBackgroundTaskWithExpirationHandler:, but i am not able to solve the problem

jww
  • 97,681
  • 90
  • 411
  • 885
Dinesh Kaushik
  • 2,917
  • 2
  • 23
  • 36
  • have you tried to use `beginBackgroundTaskWithExpirationHandler` – rckoenes Aug 07 '12 at 10:19
  • I do not know how to use beginBackgroundTaskWithExpirationHandler and where to write code for that – Dinesh Kaushik Aug 07 '12 at 10:22
  • Then why did you not search for it and implementen to see if it works? Here is nice example:http://stackoverflow.com/questions/10319643/objective-c-proper-use-of-beginbackgroundtaskwithexpirationhandler and http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html and https://www.google.nl/search?q=beginBackgroundTaskWithExpirationHandler&sugexp=chrome,mod=14&sourceid=chrome&ie=UTF-8 – rckoenes Aug 07 '12 at 10:34
  • Thanks form your valuable advise I have already gone through these links What i need is to keep wi-fi connection on for next 5 minutes By getting that 5 minutes this Application will enter background any time but will be able to complete the internet data transfer on that view controller (that can be any view controller) – Dinesh Kaushik Aug 07 '12 at 10:47
  • If you implement `beginBackgroundTaskWithExpirationHandler` you connections aren't closed, So yes the system will keep the WiFi running. On entering the background if you have used `beginBackgroundTaskWithExpirationHandler` iOS will allow you to finish what you where doing for a maximum of 10 min. – rckoenes Aug 07 '12 at 11:53

3 Answers3

0

just disable iPhone to go to sleep mode

-(void) sleepModeDisable{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

}

call this function every 10 second, this might help u

sachin
  • 1,015
  • 4
  • 11
  • 24
0

The way I handle this is to use beginBackgroundTaskWithExpirationHandler for every network request I'm sending.
This way I make sure that all my networking will completed even if my app moved to background.

I'm usually using one singleton object to handle all network request, so before the request is sent I call

- (void)startBackgroundTask
{
    // ask for extra time if this is called when app go to suspended
    UIApplication *application = [UIApplication sharedApplication];

    _bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [application endBackgroundTask:_bgTask];
        _bgTask = UIBackgroundTaskInvalid;
    }];
}  

And after I get a response (success/failure) or if I canceled the request, I call

- (void)stopBackgroudTask
{    
    UIApplication *app = [UIApplication sharedApplication];

    if (_bgTask != UIBackgroundTaskInvalid) {
        [app endBackgroundTask:_bgTask]; 
        _bgTask = UIBackgroundTaskInvalid;
    }
}  

* Don't forget to define UIBackgroundTaskIdentifier *_bgTask;

Also if you are planning to make a massive use of Wi-Fi you should set the Application uses Wi-Fi key in your plist file to YES, otherwise your Wi-Fi will be shut done after 30 minutes even if your app is running.

Eyal
  • 10,777
  • 18
  • 78
  • 130
0

No rocket science here, this is intended behavior in iOS that to save battery the Wi-Fi shuts off when phone is locked UNLESS you tell iOS that your app needs a persistant Wi-Fi, then it wont close it for you when your app is running.

For that just add UIRequiresPersistentWiFi to your info.plist and mark it YES

Documentation

Firdous
  • 4,624
  • 13
  • 41
  • 80