iphone XMPP App run background

You can indeed run a XMPP Framework-based app in the background in iOS4 by calling it a VoIP app. (However, Apple 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).


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];

Leave a Comment