Get Started

Twilio Client for iOS Quickstart Tutorial

Passing Parameters to your Application

So you've created a Twilio Application and connected to it from your iOS device -- frickin' sweet! Go get yourself a drink and celebrate!

Now, let's make a Twilio Application that can actually do something useful by passing data from your iOS app to your TwiML script. The most useful thing we could think of was... dialing out to an arbitrary phone number. It'll be easy, all we need to do is:

  • Grab the phone number that you can enter in the text field.
  • Pass that phone number along to TCDevice's -(void)connect:(NSDictionary*)parameters delegate:(id<TCDeviceDelegate>)delegate method.
  • Update the hello-client-monkey-twiml.php to serve a TwiML that contains the <Dial> verb with the phone number you passed in from your iOS device.

Let's go do it!

The text field in the app is wired up to the numberField property in HelloMonkeyViewController. We'll now add some code to pass this through to your script when creating the connection.

First, change the MonkeyPhone's -(void)connect method to take in an NSString for the phone number in MonkeyPhone.h:

MonkeyPhone.h
@interface MonkeyPhone : NSObject
{
@private
    TCDevice* _device;
    TCConnection* _connection;
}

-(void)connect:(NSString*)phoneNumber;
-(void)disconnect;

@end

And add the number in to a NSDictionary using the key "PhoneNumber" in the method implementation.

MonkeyPhone.m
-(void)connect:(NSString*)phoneNumber
{
    NSDictionary* parameters = nil;
    if ( [phoneNumber length] > 0 )
    {
        parameters = [NSDictionary dictionaryWithObject:phoneNumber forKey:@"PhoneNumber"];
    }
    _connection = [_device connect:parameters delegate:nil];
    [_connection retain];
}

And finally, in HelloMonkeyViewController.m, pass the phone number from the text field to the -(void)connect:(NSString*)phoneNumber method.

HelloMonkeyViewController.m
-(IBAction)dialButtonPressed:(id)sender
{
    HelloMonkeyAppDelegate* appDelegate = (HelloMonkeyAppDelegate*)[UIApplication sharedApplication].delegate;
    MonkeyPhone* phone = appDelegate.phone;
    
    [phone connect:self.numberField.text];
}

Now, the hello-client-monkey-twiml.php will receive a "PhoneNumber" parameter in the request when it's invoked from your app. Change the hello-client-monkey-twiml.php to read the "PhoneNumber" parameter and print that value into the <Dial> verb.

hello-client-monkey-twiml.php
<?php
header('Content-type: text/xml');

// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "XXXXXXXXXX";
?>
<Response>
    <Dial callerId="<?php echo $callerId ?>">
        <Number><?php echo $_REQUEST["PhoneNumber"]; ?></Number>
    </Dial>
</Response>

So, go ahead and compile your app for the iOS Simulator, run it, enter your cell phone number in the box and hit dial. Your phone will ring and you can talk to yourself! Huzzah!

Now, it's about time apps started calling YOU. Keep reading.


Next: Allowing Incoming Connections »