Get Started

Twilio Client for iOS Quickstart Tutorial

Receiving Incoming Calls

Now, that we know how to initiate connections from your app, it's time for your app to start receiving incoming connections! By the end of this tutorial, you will be able to call in to your device using the iOS Simulator (or vice-versa). Hold on tight...

To receive incoming connections, we'll do the following things:

  1. Give the app client a name and "register" it with Twilio.
  2. Add callbacks to notify your app of incoming connections via delegate methods.

Change your code to pass a client name to your application server when it starts up. (In a real app, you would pass a username/password combination from UITextFields, but we'll omit this for simplicity.)

TCDevice announces important events to an object that implements the TCDeviceDelegate protocol:

-(void)deviceDidStartListeningForIncomingConnections:(TCDevice*)device: callback for when the device is successfully registered with Twilio.

-(void)device:(TCDevice*)device didStopListeningForIncomingConnections:(NSError*)error: callback for when the device is no longer listening for incoming connections due to error or a call to -[TCDevice unlisten].

-(void)device:(TCDevice*)device didReceiveIncomingConnection:(TCConnection*)connection: callback for when a client receives an incoming connection

Please register the instance of HelloMonkeyViewController to be TCDevice's delegate during (id)viewDidLoad:

HelloMonkeyViewController.m
@interface HelloMonkeyViewController() <TCDeviceDelegate>
{
    TCDevice* _phone;
    TCConnection* _connection;
}
@end

@implementation HelloMonkeyViewController

- (void)viewDidLoad
{
    NSURL *url = [NSURL URLWithString:@"http://companyfoo.com/token?client=jenny"];
    NSError *error = nil;
    NSString *token = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    if (token == nil) {
        NSLog(@"Error retrieving token: %@", [error localizedDescription]);
    } else {
        _phone = [[TCDevice alloc] initWithCapabilityToken:token delegate:self];
    }
}
@end

Next, implement -(void)device:(TCDevice*)device didReceiveIncomingConnection:(TCConnection*)connection and have it accept the incoming connection when we get the callback. Note that we can only accept incoming connection when the device is not busy.

HelloMonkeyViewController.m
- (void)device:(TCDevice *)device didReceiveIncomingConnection:(TCConnection *)connection
{
    if (device.state == TCDeviceStateBusy) {
        [connection reject];
    } else {
        _connection = [connection accept];
    }
}

For completeness, implement the remaining two delegate methods to print out various state transitions and any errors (note that -device:didStopListeningForIncomingConnections: is a required method for the delegate):

HelloMonkeyViewController.m
- (void)deviceDidStartListeningForIncomingConnections:(TCDevice*)device
{
    NSLog(@"Device: %@ deviceDidStartListeningForIncomingConnections", device);
}

- (void)device:(TCDevice*)device didStopListeningForIncomingConnections:(NSError*)error
{
    NSLog(@"Device: %@ didStopListeningForIncomingConnections: %@", device, error);
}

If you compile and run the app, it's now ready to receive incoming calls! Now we need to provision a Twilio phone number and configure the Voice URL to point to your application server: http://{your app's url}/call.

Call your Twilio number and you should be connected to your device (or iOS Simulator, whichever you're running), which will automatically answer the call! Start talkin'.

Ready to put it all together?


Next: Making Calls in/out of Your App »