如果你的应用程序需要在设备休眠的时候还能够收到服务器端发送的消息,那我们就可以借助VOIP的模式来实现这一需求。但是如果的应用程序并不是正真的VOIP应用,那当你把你的应用提交到AppStore的时候基本上会被苹果Reject. 但是如果你的应用是企业内部发布的或者你只想了解其中的原理,那该文也许对您会有所帮助。
一、在iOS中如何应用VOIP
大多数VOIP应用需要设置后台audio 应用去传递音频,因此你应该设置audio 和voip两个键值。如果只是想通过VOIP来达到socket在休眠状态下维持长连接那只需要象上图一样设置VOIP即可。
接口
1、NSInputStream 和NSOutputStream 
NSStreamNetworkServiceType  
stream.
  
改属性的值设为  
NSStreamNetworkServiceTypeVoIP.  
2、NSURLRequest 
method of your NSMutableURLRequest object to set the network service  
type of the request. The service type should be set to  
NSURLNetworkServiceTypeVoIP. 
For Core Foundation streams, use the CFReadStreamSetProperty or  
CFWriteStreamSetProperty function to add the kCFStreamNetwork-  
ServiceType property to the stream. The value for this property should be  
set to kCFStreamNetworkServiceTypeVoIP. 
由于,VOIP应用程序需要一直运行以确保收到来电,所以如果程 序通过一个非零的exit code退出,系统将自动重启这个应用程序(这种退出方式可以发生在内存压力大时终止程序运行)。尽管如此,中断应用程序会release所有的
sockets,包括那个用于连接voip 服务的socket。因此,当程序运行时,它需要一直从头创建socket。
当你建立了handler之后,确定应用程序所需的最大超时。系统保证会在最大超时之前调用handler,但是这个时间是不确定的,所以你的handler必须在你申明的超时之前做好执行程序的准备。
二、通过GCDAsyncSocket来实现休眠状态下的长连接:
1、在创建socket并且成功连接上远程服务器的delegate中执行下述代码
  1. - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)connectedPort{
  2. [socket performBlock:^{
  3. [socket enableBackgroundingOnSocket];
  4. }];
  5. }
2、在ApplicationDidEnterBackground方法中调用setKeepAliveTimeOut以保持长连接
  1. BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
  2. if (backgroundAccepted)
  3. {
  4. NSLog(@"VOIP backgrounding accepted");
  5. }
参考:
1、XMPP中长连接问答
2、GCDAsyncSocket实现后台长连接的VOIP问答
3、后台运行(内容比较杂,但还是很有参考价值)
4、有一篇国内的关于VOIP以及后台程序的博文(内容还是比较杂--还是英文的,估计只能由博主自己看懂,仅做参考)
http://blog.csdn.net/gnicky/article/details/7452418
5、iOS socket编程基础
6、一篇StackOverflow.com中的问答,其中对iOS中对VOIP的解释比较详细

If
you want to let your VOIP application run in background , except those
base settings in plist file, you need a TCP socket who's property is set
to VOIP, than the iOS system will
take care this socket for you, when your application enter background ,
every thing was 'sleep' except that tcp socket. and if VOIP server send
some data thought that TCP socket, your application will be awake up
for 10 secs. during this time, you can post
a local notification.


Only
Tcp socket can be set as VOIP Socket. But from i know , mostly VOIP
application are based on UDP socket. if you do not want to separate the
control socket from the data socket.
you should create another tcp socket which is focus on 'awake' your
application , and from my personal experience , it's very hard to keep
this 'awake' signal and the real sip control signal synchronize, the
application always miss the sip invite request.

So,the
best way is separating the sip control single from the UDP data socket ,
make it as a tcp socket , this is the best solution , but never use tcp
socket to transfer voice data.

Another
dirty way: keep the application awake all the time. As i said , each
TCP single the application received thought that 'VOIP' tcp socket ,
will keep application awake for
10 seconds, so at the end of this duration(after 9 secs) , you can send
a response to the server to ask for another signal , when the next
signal arrived, the application will be awake again,after 9 secs , send
response again. keep doing this, your application
will awake forever.
7.CocoaAsyncSocket

https://github.com/robbiehanson/CocoaAsyncSocket

iOS应用中通过设置VOIP模式实现休眠状态下socket的长连接的更多相关文章

  1. [转] iOS应用中通过设置VOIP模式实现休眠状态下socket的长连接

      转自:http://blog.csdn.net/missautumn/article/details/17102067 如果你的应用程序需要在设备休眠的时候还能够收到服务器端发送的消息,那我们就可 ...

  2. ios系统中各种设置项的url链接

    ios系统中各种设置项的url链接 在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplic ...

  3. ios开发中全局变量设置和调用方法

    ios开发中,全局变量设置和调用方法如下:在AppDelegate.h文件中设置全局变量:@interface ***AppDelegate{NSString *myName;}@property ( ...

  4. [ios2]ios系统中各种设置项的url链接

    在代码中调用如下代码:(ps: ios 5.0 以后不可用)NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIAppli ...

  5. IOS 应用中从竖屏模式强制转换为横屏模式

    在 iPhone 应用里,有时我们想强行把显示模式从纵屏改为横屏(反之亦然),CocoaChina 会员 “alienblue” 为我们提供了两种思路 第一种:通过人为的办法改变view.transf ...

  6. [iOS] iOS系统中各种设置项的url链接

    在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplication sharedApplic ...

  7. 在IOS应用中从竖屏模式强制转换为横屏模式

    http://www.cnblogs.com/mrhgw/archive/2012/07/18/2597218.html 在 iPhone 应用里,有时我们想强行把显示模式从纵屏改为横屏(反之亦然), ...

  8. IOS中快速集成短信SDK验证开发(SMSSDK),IOS开发中如何设置手机验证码

    [转载请注明出处] sdk是别人的,我只是下载来集成一下. smssdk下载网站:http://www.mob.com/(也有其他很多网站有类似SDK,譬如https://www.juhe.cn/等等 ...

  9. 如何在 iOS 8 中使用 Swift 实现本地通知(下)

    在上集中,我们已经构建了一个简单的待办列表应用(to-do list app),这个应用可以在待办项过期时通过本地通知提醒用户.现在,我们要在之前的基础上添加以下功能:应用图标角标上显示过期待办项的数 ...

随机推荐

  1. (12)odoo各种提前期和时间

    1)Product的提前期    Customer Lead Time(sale_delay):客户提前期,指SO确认到向客户发货的天数,由于销售数量不同该时间也不同,因此,这里是一个平均时间.    ...

  2. JAVA String作业——动手动脑以及课后实验性问题

    一:如何解释这样的输出结果?从中你能总结出什么?1.源码 //mao2015.10.20 //==的两个分辨 //对原始数据而言 //对引用类型变量 public class StringPool { ...

  3. PHP程序员面试技巧之口试题分享

    网络上流传很广的一部分php工程师面试题目,有些phper们认为这些很形式,天下面试题目一大把,不能考核一个人的真实水平,其实细细研究起来,无论怎样,能存在就表明其有存在的价值.下面小编整理了12条P ...

  4. jsp之 ---- 页面重定向和请求转发(笔记之深度说明)

    1.  HttpServletResponse对象的sendRedirect(String location)方法称作重定向. 如果location地址前面加上“/”,则表示  相对于Servlet容 ...

  5. java SE学习之线程同步(详细介绍)

           java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题:               当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...

  6. android:clipToPadding和android:clipChildren

    假设我们要做一个效果,界面最顶部是一个ActionBar并且是半透明的,ActionBar下面是一个ListView,在初始状态 下, ListView是top是在ActionBar的bottom位置 ...

  7. javascript 对象和数组(花括号、方括号)

    1. javascript对象定义 var a = {var a = "test", var b = "test"} 2. javascript数组定义 var ...

  8. pycharm 导包

    如果需要requests包,python没有自带.可以执行命令:pip install requests,自动安装导入.

  9. Linux 服务器安全技巧

    毋庸置疑,对于系统管理员,提高服务器的安全性是最重要的事情之一.因此,也就有了许多针对这个话题而生的文章.博客和论坛帖子. 一台服务器由大量功能各异的部件组成,这一点使得很难根据每个人的需求去提供定制 ...

  10. bzoj 2242: [SDOI2011]计算器

    #include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...