4

I'm working on a socket based client-server app.

When a user running any iDevice on iOS 4, 4.2.1, 4.3.2, etc clicks the lock button while connected to the server, the connection remains alive.

However, when I click the lock button on any device running iOS 5, the connection is immediately destroyed and upon returning to the app from the lock screen I am presented with my NSAlertView that is called when the NSStreamEventErrorOccurred method is called.

I have had several clients test the app, and every one of them with iOS 5 has this exact same problem - no matter the device (iPod 2g, iPod 4g, iPhone 3GS, iPhone 4).

Were there any changes to the way iOS 5 devices handle locks? How can I fix this?

EDIT:

I should mention the project was started in Xcode 4 (iOS 4 sdk) and is now being used in Xcode 4.2.. Don't know if this matters or not.

James V
  • 189
  • 1
  • 2
  • 10
  • 2
    possible duplicate of [iphone app network connection disconnect after screen locking with new ios sdk 5.0](http://stackoverflow.com/questions/7866651/iphone-app-network-connection-disconnect-after-screen-locking-with-new-ios-sdk-5) –  Nov 25 '11 at 15:02
  • It would be interesting to know whether this is unique to 5.0.1 or whether 5.0 had the same behaviour given the battery saving 'bug fixes' Apple added in 5.0.1 – Dolbz Nov 25 '11 at 15:03
  • ah from the look of the duplicate it would appear it's not 5.0.1 specific and it is a iOS 5 feature – Dolbz Nov 25 '11 at 15:04
  • Yes, we are experiencing the problem on both iOS 5 and 5.0.1 – James V Nov 25 '11 at 15:26

2 Answers2

1

As in the comments, there is a new 'feature' in iOS 5 which sends the application to the background when the user locks the device, this effectively kills network activities.

Use beginBackgroundTaskWithExpirationHandler: to mark critical sections of the code that need the socket access. This should allow the code to run while the app is in the background for a short while. Dont forget to call endBackgroundTask when you are done.

Robert
  • 37,670
  • 37
  • 171
  • 213
0

Actually i don't think in iOS 5, when you lock the device an active application will be sent to the background mode, it will just become inactive. I have done some tests, if you have a network download started, then send the app to background, the network socket is not killed, but suspended. you could wait 10+ seconds( 10 seconds is supposed to the default extended time your app has before your app become suspended in the background), then bring the app back to foreground, you can see the halfway done network download resumes. While if you lock the device and unlock immediately, you will encounter network error because your socket has been disconnected. This tells me ios 5 doesn't just send your app to the background when the device is lock, it will also disconnect your network socket right away(using the extended background task time probably wouldn't help you)

Kitty
  • 11
  • When the device screen turned Off, My iOS app stops sending data to socket. and then turning ON the screen back, socket connection gets disconnected / broken pipe error. How to solve it? – Stella Feb 22 '14 at 21:22