6

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?

HDdeveloper
  • 4,396
  • 6
  • 40
  • 65
  • This doesn't look like a VOIP app. Q1: is it? Q2: is this app ment for app store or private use? – Rok Jarc Jun 14 '12 at 12:29
  • One more thing: even is you use voip mode you need to test in on device. It will not work on simulator. – Rok Jarc Jun 14 '12 at 12:31
  • Hi @rokjarc, this socket connection is between server and client. I want to show local notification to the user when server sends the message to client, i.e. I want to use the - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent in background - (void)applicationDidEnterBackground:(UIApplication *)application I have done all the changes necessary according to apple document. Please let me know if you have the anser – HDdeveloper Jun 14 '12 at 12:59
  • Well, i guess that's not the answer you were hoping for but i don't see any other possibility. Hopefully someone else will be of more help. – Rok Jarc Jun 14 '12 at 13:18
  • @rokjarc, Thanks for helping.I am not using the device that might be the reason, its not showing control on the NSStream's delegates. Waiting for apple approval to test it on device. thanks once again. – HDdeveloper Jun 14 '12 at 13:48
  • You're welcome - and yes, this (using simulator) is probably the real culprit. – Rok Jarc Jun 14 '12 at 13:49

2 Answers2

10

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

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • @rokjarc then how to display alert to use if app is in background and get events..... – user100 Apr 22 '14 at 13:05
  • This would usually be achieved by sending APNS push notification to a device. – Rok Jarc Apr 22 '14 at 14:01
  • Will apple reject if an app is VoIP and plays a silent audio in background now? – iOS Monster May 15 '14 at 18:28
  • 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. – Rok Jarc May 15 '14 at 21:44
0

Use this code to keep your app alive in ios

var backgroundUpdateTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)

func endBackgroundUpdateTask() {
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
func applicationWillResignActive(_ application: UIApplication) {
    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}
func applicationDidBecomeActive(_ application: UIApplication) {
    self.endBackgroundUpdateTask()

}

i hope you will get success by this help

Rohit Nishad
  • 428
  • 4
  • 7