iphone XMPP App run background

11,093

Solution 1

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).

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

Solution 2

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

Solution 3

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

Share:
11,093
Raj
Author by

Raj

Mobile application developer

Updated on July 28, 2022

Comments

  • Raj
    Raj almost 2 years

    I created a chat application using XMPP framework..when I quit the app(enter background mode) I want to receive the chat message..and also need to display the icon badge...How can I do this?

  • Raj
    Raj over 13 years
    first of all thank you for you replay..i dont want to push this app to appstore.using IPA file for distribution..i read the link you given and added VOIP in info.plist..need any more change in code..beacuse its still not work in backkground..
  • Ash
    Ash over 13 years
    I think you've skim-read the document I pointed you to; you need to do more than simply add VOIP to the info.plist in order to make it work. Look further down the page under "Implementing a VOIP Application"
  • Raj
    Raj over 13 years
    thank you Jeff , This is working fine for me ..thanks a lot:-)
  • subharb
    subharb over 12 years
    My application is set as an Location based app, but after 3 hours in the background it disconnects from my XMPP server. Besides setting the info.plist as location is there something else to set in the xmppframework so that it never disconnects?
  • Ash
    Ash over 12 years
    That I can't answer for certain, but you might want to make sure of the reasons your app is disconnecting. Could it be server timeout? Could there be a memory issue that's causing the device to terminate your app's runtime?
  • iphonedev23
    iphonedev23 about 11 years
    control is not going to "socketWillConnect:(AsyncSocket *)socket" method. Please help
  • Jeff Hay
    Jeff Hay about 11 years
    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)
  • Jayeshkumar Sojitra
    Jayeshkumar Sojitra about 9 years
    @JeffHay Do you have any idea how can I implement the same functionality without using VOIP flag? It would be very helpful for me to solve my issues..
  • Chirag Lukhi
    Chirag Lukhi over 8 years
    If I user VOIP here,then will that app rejected by Apple? Or it will approved by Apple as mention following lines as per your said. CFReadStreamSetProperty([socket getCFReadStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP); CFWriteStreamSetProperty([socket getCFWriteStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);