Locking iPhone disconnects sockets on iOS 5 only

旧街凉风 提交于 2019-12-22 08:30:40

问题


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.


回答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.




回答2:


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)



来源:https://stackoverflow.com/questions/8270860/locking-iphone-disconnects-sockets-on-ios-5-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!