Dial a Phone Number – So Many Options

I am aware of three ways to dial a phone number from within an iPhone. The first was shared by Rodney in October of 2008: Launching Other Apps within an iPhone Application.

As mentioned in the above post, the UIApplication class offers a method to dial a phone number:

1
2
[[UIApplication sharedApplication] 
    openURL:[NSURL URLWithString:@"tel://18004664411"]];

The second way to dial a phone number I stumbled upon when using a UIWebview to display formatted text within an information screen of an application. In the HTML stream, I included a phone number and was caught by surprise to see that the number appeared as a hyperlink in the application, and by touching the number, I was prompted if I wanted to place a call. You can see how this looks in a very simple webview with a white background:

This of course led to looking a little further (read, opening the documentation) and sure enough, for iPhone OS 2+ there is a property detectsPhoneNumbers that looks for numbers that appear to be formatted as phone numbers. This property is on by default. For iPhone OS 3+ this property has been deprecated (more on dealing with deprecated methods) and replaced with the property dataDetectorTypes which offers a range of options for detecting phone numbers, links, etc.

The third approach is a combination of the URL idea used with UIApplication along with a UIWebview and a hyperlink. With this approach you can display text that represents a phone number, versus the actual numbers. See the screenshot below:

What follows is a short example that shows the two options for dialing a number from within a UIWebview.

Keeping things short and sweet, this example using one class, the app delegate, here’s how the applicationDidFinishLoading method looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{        
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
  // Define the frame (location/size) for the webview
  UIWebView *webView = 
      [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 35)];  
  webView.backgroundColor = [UIColor whiteColor];
  [webView setOpaque:NO];
 
  // Access the html file
  NSString *textpath = @"about.txt";
  NSString *filePath =
       [[NSBundle mainBundle] pathForResource:textpath ofType:nil];
  NSString *html = [NSString stringWithContentsOfFile:filePath];  
 
  // Define the html body info
  NSString *htmlOpen = @"<body>";
  NSString *htmlClose = @"</body>"; 
 
  // The overall document structure
  NSString *bodyhtml = 
      [NSString stringWithFormat:@"%@ %@ %@", htmlOpen, html, htmlClose];
 
  // Load the html in the webview
  [webView loadHTMLString:bodyhtml baseURL:nil];   
 
  // Add webview to current view
  [window addSubview:webView];
 
  // self.view retained the webview, we can release it
  [webView release];
 
  [window makeKeyAndVisible];
}

I setup a typical HTML body tag (open and close) and reference the file about.txt which holds the HTML for the phone numbers. The file about.txt is quite simple, and looks as follows:

<center>
<p>1&#45;800&#45;466&#45;4411</p>
 
<a href="tel://8004664411">GOOG-411</a>
</center>

If you are aware of other ways to dial a phone number from within an iPhone application, please post a comment.

  1. iPhone OS 2+ used to make it so easy to post a number in a UIWebView. Now, with detectsPhoneNumbers being deprecated, I am stuck trying to find suitable documentation for dataDetectorTypes. Appl’s docs are so matter-of-fact with little to no examples given.
    You would think that webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber; would work simply enough, but, even tho no errors are give, no phone number links are made. Am i missing something?

  2. I think there is a forth way but I can’t figure it out. The Contacts app can send a number to the Phone app.

    The cool thing about the way the Contacts app does it is that it can send a # (pound).

    This means that I can setup a Contact that contains my conference call number and codes and not have to touch the phone once (use voice control).

    Now here is the question: how can I send a phone number to the Phone app the way the Contacts app does it? i.e. send # and , and * key presses

Comments are closed.