views:

553

answers:

3

I'm trying to implement the background mode for VoIP application using the new mechanisms provided by iPhone OS 4.x

Still without success... Application remains silent in the background mode when some data arrives from the server via TCP socket.

According to the documentation the following two things have to be done:

  1. Add "voip" value for "Required background modes" in Info.plst file
  2. Set property "kCFStreamNetworkServiceType" of CFReadStreamRef object to "kCFStreamNetworkServiceTypeVoIP" value

I'm using an externally created BSD socket to instantiate CFReadStreamRef with CFStreamCreatePairWithSocket function. Socket handle is correct - checked with some testing. So I'm obtaining a correct CFReadStreamRef object, which I'm later configuring and opening.

Here is the chunk of code:

CFSocketNativeHandle socket = (CFSocketNativeHandle)socketHandle; 
CFStreamCreatePairWithSocket(kCFAllocatorDefault, socket, &sipSocketReadStream, nil);
CFReadStreamSetProperty(sipSocketReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
NSInputStream *inputStream = (NSInputStream*)sipSocketReadStream;   
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFReadStreamOpen(sipSocketReadStream);

Please note - my delegate for NSInputStream receives successfully "Stream Opened" event. But any other events simply didn't come - I was expecting to receive an event when some TCP traffic started to arrive.

Please help me to resolve this problem.

Thanks!

A: 

Have you got your problem solved? Guess CFReadStreamSetProperty is failed. But why is it?

Andrew
A: 

You need to make sure you have "UIBackgroundModes" as an array, with the "voip" option in your plist file:

<key>UIBackgroundModes</key>
<array>
    <string>voip</string>
</array>

In addition, voip background tasks do NOT work under the simulator, so make sure you test with a real device. If you did set the UIBackgroundModes properly and set the stream type to kCFStreamNetworkServiceTypeVoIP, then my only conclusion is that you are probably testing with the simulator.

Good luck!

Gilad
A: 

I must reiterate the sage advice mentioned by Gilad. The background TCP sockets do NOT work on the Simulator. I spent quite a while trying to get to the bottom of this problem, sucked in by the convenience of the Simulator.

In addition, and maybe its just me, I didn't find it clear from the documentation which value was the key and which value was the property. For the record, this is how I set it and it works:

res = [oStream setProperty:NSStreamNetworkServiceTypeVoIP 
                    forKey:NSStreamNetworkServiceType];

if(!res){
   // Error handling here
}
Brian Kelly