How to handle socket connection's events when app is in background?

21,035

I was dealing with similiar problem a while ago. Few important things to keep in mind:

  • background "voip" functionality only works on device - don't use simulator to test it
  • you will probably (tested) got rejected if your app registers as a voip app and isn't really voip app

So if this is not a voip app you might actually want to use remote notifications to alert user directly rather than showing local notification. I guess this is the only way for your app to pass App Store validation.

Anyway, two links here on SO helped you might find helpful:

How can an iOS app keep a TCP connection alive indefinitely while in the background?

I ended up using voip (as you do) and playing silent audio loop as suggested here - it worked. Not sure if this silent audio loop is still neccessary.

What happens to TCP and UDP (with multicast) connection when an iOS Application did enter background

Make sure you read Tips for Developing a VoIP App and Technical Note TN2277:Networking and Multitasking

Share:
21,035
HDdeveloper
Author by

HDdeveloper

Updated on July 09, 2022

Comments

  • HDdeveloper
    HDdeveloper almost 2 years

    I want use the following function even when app is in background?

    - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
     {
            case NSStreamEventHasBytesAvailable:
            {  NSLog(@"Event:NSStreamEventHasBytesAvailable");
                if (theStream == _inputStream) {
    
                    NSLog(@"NSStreamEventHasBytesAvailable: on Input Stream");
                    uint8_t buffer[1024];
                    int len;
    
                    while ([_inputStream hasBytesAvailable]) {
                        len = [_inputStream read:buffer maxLength:sizeof(buffer)];
                        if (len > 0) {
    
                            NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
    
                            if (nil != output) {
    
                                NSLog(@"server said: %@", output);
                                 // to get local notification I am calling below method.
     [self scheduleNotification];       
                            }
                        }
                    }
                }
                break;
            }
    

    The above code is working done in foreGround. I have made all the change given in apple document to the run the app in the background mode- voip. What should i write in AppDelegate method?

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    }
    

    How to get the stream:handleEvent called in background?

  • user100
    user100 about 10 years
    @rokjarc then how to display alert to use if app is in background and get events.....
  • Rok Jarc
    Rok Jarc about 10 years
    This would usually be achieved by sending APNS push notification to a device.
  • iOS Monster
    iOS Monster about 10 years
    Will apple reject if an app is VoIP and plays a silent audio in background now?
  • Rok Jarc
    Rok Jarc about 10 years
    If app is actually a VoIP app then it has "legite" rights to run in background - you shouldn't have to fiddle with "silent audio" workarounds. Problematic apps are those that introduce themselves as VoIP apps but in reality have nothing to do with VoIP.