iOS Quickstart: Passing Parameters to your Application

Passing Parameters to your Application to Make a VoIP Call

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

  1. Grab a phone number from a text field.
  2. Pass that phone number along to TCDevice's -(void)connect:(NSDictionary*)parameters delegate: (id<TCDeviceDelegate>)delegate method.
  3. Update your TwiML Application to point to the back-end endpoint that receives this input and dials the number entered by the user.

Let's go do it!

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

In HelloMonkeyViewController.m, create a NSDictionary* with an object containing key To and value of the text field. Pass this dictionary to the connect method.

- (IBAction)dialButtonPressed:(id)sender
    NSDictionary *params = @{@"To": self.numberField.text};
    _connection = [_phone connect:params delegate:nil];

Now, let's change our TwiML Application to point to our app's /call endpoint. Go to the TwiML apps page in the account portal, change your application's Voice URL to point to your server's /call endpoint: http://{your server url}/call, and save your changes.

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

Here's how it works. When your application connects to Twilio, your TwiML App's Voice URL points to the /call endpoint on your python server, which runs the following code:
@app.route('/call', methods=['GET', 'POST'])
def call():
  resp = twilio.twiml.Response()
  from_value = request.values.get('From')
  to = request.values.get('To')

  if not (from_value and to):
    return str(resp.say("Invalid request"))

  from_client = from_value.startswith('client')
  caller_id = os.environ.get("CALLER_ID", CALLER_ID)

  if not from_client:
    # PSTN -> client
  elif to.startswith("client:"):
    # client -> client
    # client -> PSTN
    resp.dial(to, callerId=caller_id)

  return str(resp)

This code looks at the From parameter in the request from Twilio. If the connection is coming from your iOS application, it grabs the To parameter passed by your application and dials the number.

Things work a bit differently if the call is coming from a PSTN phone number. Let's dig into that now: Now, it's time to get your app ringing.

Next: Receiving Incoming Calls »