Long connection of sockets in hibernation by setting VoIP mode in iOS app

Source: Internet
Author: User
Tags vars

if your application needs to be able to receive messages sent from the server while the device is hibernating, then we can use the VoIP mode to achieve this requirement. But if the application is not a true VoIP app, then when you submit your app to AppStore it will basically be reject by Apple. But if your app is published internally or you just want to know how it works, it might help.
first, how to apply VOIP in iOS


Most VoIP apps need to set up a background audio app to deliver audio, so you should set audio and VoIP two key values. If you just want to use VoIP to reach the socket in a dormant state to maintain a long connection that only need to set up VoIP as the same.



Interface
1, Nsinputstream and Nsoutputstream
NsstreamnetworkservicetypeStream. the value of the Change property is set toNsstreamnetworkservicetypevoip. 2, NsurlrequestMethod of your Nsmutableurlrequest object to set the network service
type of the request. The service type should is set toNsurlnetworkservicetypevoip.for Core Foundation streams, use the Cfreadstreamsetproperty orcfwritestreamsetproperty function to add the kcfstreamnetwork-ServiceType property to the stream. The value for this property should isset to Kcfstreamnetworkservicetypevoip.
because VoIP applications need to run continuously to ensure that incoming calls are received, if the program exits with a nonzero exit code, the system will automatically restart the application (this exit can occur when the memory pressure is high to terminate the program). However, the interrupt application will release all sockets, including the socket used to connect to the VoIP service. Therefore, when the program runs, it needs to create the socket from scratch.



when you have established handler, determine the maximum time-out required for the application. The system guarantees that handler will be called before the maximum time-out, but this time is indeterminate, so your handler must be ready to execute the program before you declare the timeout.
second, through the gcdasyncsocket to realize the long connection in the dormant state:
1. Execute the following code in the delegate that created the socket and successfully connected to the remote server [OBJC]View Plaincopyprint?
    1. -(void) Socket: (gcdasyncsocket *) sock didconnecttohost: (NSString *) host port: (UInt16) connectedport{
    2. [Socket performblock:^{
    3. [Socket Enablebackgroundingonsocket];
    4. }];
    5. }



2. Call Setkeepalivetimeout in the Applicationdidenterbackground method to maintain a long connection [OBJC]View Plaincopyprint?
    1. BOOL backgroundaccepted = [[uiapplication sharedapplication] setkeepalivetimeout:600 Handler  : ^{[self Backgroundhandler];}];
    2. if (backgroundaccepted)
    3. {
    4. NSLog (@ "VOIP backgrounding accepted");
    5. }



Reference:1. XMPP Middle-length connection question and answerHttp://stackoverflow.com/questions/5257580/iphone-xmpp-app-run-background

2. Gcdasyncsocket VoIP question and answer Http://stackoverflow.com/questions/8748874/gcdasyncsocket-background-voip for long-connected backstage

3, the background operation (the content is miscellaneous, but it is still very valuable reference) http://blog.csdn.net/ssihc0/article/details/7240495

4, there is a domestic on the VoIP and Backstage program blog (the content is still relatively miscellaneous-or English, estimated only by the Bo master himself to understand, only for reference) http://blog.csdn.net/gnicky/article/details/7452418

5, IOS Socket Programming Foundation http://www.raywenderlich.com/3932/ Networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server

6, a stackoverflow.com in the question and answer, which on the iOS in the interpretation of VoIP more detailed

If you want to let your VOIP application run in background except those base settings in plist file, you need a TCP sock Et who's property was set to VOIP, than the IOS system would take care this socket for you, when your application enter back Ground, every thing is ' sleep ' except that TCP socket. And if VOIP server send some data thought that TCP socket, your application would be the awake up for ten secs. During this time, you can post a local notification.


Only the TCP socket can be set as a VOIP socket. But from I know, mostly VOIP application is based on UDP socket. If you don't want to separate the control socket from the data socket. You should create another TCP socket which are focus on ' awake ' your application, and from my personal experience, it ' s V Ery hard-to-keep this ' awake ' signal and the real SIP control signal Synchronize, the application always miss the SIP Invi Te request.
so,the best-separating the SIP control single from the UDP data socket, make it as a TCP socket, the Best solution, but never use TCP sockets to transfer voice data.
another dirty way:keep the application awake all the time. As I said, each TCP a application received thought that ' VOIP ' TCP socket, would keep application awake for ten S Econds, so at the end of this duration (after 9 secs), you can send a response to the server to ask for another signal, W Hen the next signal arrived, the application would be awake again,after 9 secs, send response again. Keep doing this, your application would awake forever.Http://stackoverflow.com/questions/6601536/ios-voip-socket-will-not-run-in-background

7.CocoaAsyncSocket

Https://github.com/robbiehanson/CocoaAsyncSocket

Long connection of sockets in hibernation by setting VoIP mode in iOS app

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.