How to keep the socket long connection and data transfer in iOS background
Source: Internet
Author: User
<span id="Label3"></p><p><p>The project uses a TCP long connection to data transfer with the server, on the iOS platform, due to Apple's background mechanism, There are the following issues:</p></p><p><p>When the program is back in the background, all the threads are suspended and the system reclaims all the socket resources, then the socket connection is closed and the data can no longer be transferred:</p></p><p><p></p></p><p><p>Workaround:</p></p><p><p>You can maintain the socket connection and the continuation of data transfer by setting the following properties</p></p><p><p>1. Need to add the VoIP key value in the Uibackgroundmodes in the Info.plist file;</p></p><p><p>2. Set flow Properties</p></p><p><p>Cfreadstreamref and Cfwritestreamref set the Kcfstreamnetworkservicetype property to Kcfstreamnetworkservicetypevoip by the following method;</p></p><p><p></p></p><p><p>Cfreadstreamsetproperty (thereadstream, kcfstreamnetworkservicetype, kcfstreamnetworkservicetypevoip);</p></p><p><p>Cfwritestreamsetproperty (thewritestream, kcfstreamnetworkservicetype, kcfstreamnetworkservicetypevoip);</p></p><p><p></p></p><p><p>Nsinputstream and Nsoutputstream set the Nsstreamnetworkservicetype property to Nsstreamnetworkservicetypevoip by the following method;</p></p><p><p></p></p><p><p>[self.stream Setproperty:nsstreamnetworkservicetypevoip forkey:nsstreamnetworkservicetype];</p></p><p><p></p></p><p><p>3. Here is a problem, that is, the client is through the heartbeat and the server to maintain the connection, the heartbeat is triggered by the timer, when I retire to the background, the timer method is suspended, then the following settings to run the timer in the background</p></p><p><p></p></p>? <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td>123456789101112131415161718192021</td> <td><code><code>- (</code></code><code><code>void</code></code><code><code>)applicationDidEnterBackground:(UIApplication*)application{</code></code><code><code> </code></code><code><code>UIApplication* app = [UIApplication sharedApplication];</code></code><code><code> </code></code><code><code>__block UIBackgroundTaskIdentifier bgTask;</code></code><code><code> </code></code><code><code>bgTask= [app beginBackgroundTaskWithExpirationHandler:^{</code></code><code><code> </code></code><code><code>dispatch_async(dispatch_get_main_queue(),^{</code></code><code><code> </code></code><code><code>if</code></code><code><code>(bgTask != UIBackgroundTaskInvalid)</code></code><code><code> </code></code><code><code>{</code></code><code><code> </code></code><code><code>bgTask= UIBackgroundTaskInvalid;</code></code><code><code> </code></code><code><code>}</code></code><code><code> </code></code><code><code>});</code></code><code><code> </code></code><code><code>}];</code></code><code><code> </code></code><code><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,</code></code><code><code>0</code></code><code><code>),^{</code></code><code><code> </code></code><code><code>dispatch_async(dispatch_get_main_queue(),^{</code></code><code><code> </code></code><code><code>if</code></code><code><code>(bgTask != UIBackgroundTaskInvalid)</code></code><code><code> </code></code><code><code>{</code></code><code><code> </code></code><code><code>bgTask= UIBackgroundTaskInvalid;</code></code><code><code> </code></code><code><code>}</code></code><code><code> </code></code><code><code>});</code></code><code><code> </code></code><code><code>});</code></code><code><code>}</code></code></td> </tr> </tbody> </table><p><p>How to keep the socket long connection and data transfer in iOS background</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service