iOS XMPP App run background (by voip)

本文转自stackoverflo:http://stackoverflow.com/questions/5257580/iphone-xmpp-app-run-background



up vote 8 down vote accepted

You can indeed run a XMPP Framework-based app in the background in iOS4 by calling it a VoIP app. (HoweverApple will reject it from the App Store unless it also truly does VoIP).

You need to set the VoIP flag in your app's (appname)-info.plist file, and then in

(void)xmppStream:(XMPPStream *)sender socketWillConnect:(AsyncSocket *)socket

You'll need to set the socket stream flags to include kCFStreamNetworkServiceTypeVoIP:

 CFReadStreamSetProperty([socket getCFReadStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
 CFWriteStreamSetProperty([socket getCFWriteStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);

Then, your app will be woken up briefly when a new XMPP message arrives. In your normal

(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

handler, you would want to create a local notification for the message if you are backgrounded (you can keep track of background state via UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification). The local notification handler can set the application badge number, etc (just like you would for a push notification).

EDIT

Newer versions of the XMPP Framework (specifically, GCDAsyncSocket) now support a call to make this easier, so you can just have:

- (void)xmppStream:(XMPPStream *)sender socketWillConnect:(GCDAsyncSocket *)socket
{
    // Tell the socket to stay around if the app goes to the background (only works on apps with the VoIP background flag set)
    [socket performBlock:^{
            [socket enableBackgroundingOnSocket];
    }];
}
share | improve this answer
 
 
thank you Jeff , This is working fine for me ..thanks a lot:-) –   Raj  Mar 11 '11 at 6:07
 
control is not going to "socketWillConnect:(AsyncSocket *)socket" method. Please help –   iphonedev23  Apr 23 '13 at 7:15
 
Control should go to socketWillConnect: as long as you have called addDelegate: on your XMPPStream object. Note that the socketWillConenct: call is now expecting a GCDAsyncSocket* parameter rather than AsyncSocket* (as of XMPPFramework 3) –   Jeff Hay  Apr 23 '13 at 15:08

There are a limited number of programs that can run in the background without limit, these being VOIP programs, those that play music, and those that track the user's location. If you're not doing any of these legitimately then you're limited to ten minutes of background operation. Note that Apple will reject apps that try silly tricks like playing 'empty' sounds to keep the app live.

You can find info on running tasks in the background here:http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Your other option I would guess is to have the chat program operate by contacting a server, and to have that server queue responses when a user is offline then deliver them when they next log on. Not being a net programmer myself I don't know how feasible this is, but I think it's the only way to do this indefinitely if you're only offering text chat. The better option would be to make your application VOIP enabled using the guide above.

EDIT As of the release of iOS 5.0, it is also possible to get apps registered as Newsstand applications to download information while backgrounded, plus a bunch of other funky features that are also Newsstand only.

-Ash


In the latest XMPP Framework you don't need to modify framework files.

Just do this: 1. Add this to your connect method

#if !TARGET_IPHONE_SIMULATOR
{
    self.xmppStream.enableBackgroundingOnSocket = YES;
}
#endif

2. Add voip key to your info plist file:

enter image description here


第三种的方法是最新的, xmpp framework框架中已经写好了voip的socket后台连接 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值