Detecting phone numbers and links in an iPhone application
Q: How do I detect phone numbers and links in an iPhone application?
A: How do I detect phone numbers and links in an iPhone application?
As of iPhone OS 3.0, text views and web views support the detection of phone numbers, email addresses, and HTTP links. iPhone OS 3.0 scans for these types of data, highlights them when found, and renders them into clickable URLs as shown in Figure 1. You can detect phone numbers and links programmatically or by using Interface Builder 3.1.2 or later.
Programmatically detecting phone numbers and links
The iPhone OS SDK 3.0 introduces the UIDataDetectorTypes
enumeration constant, which defines the types of data that can be found in web views and text views. Table 1 provides a description of these data types.
Constant | Description |
---|---|
UIDataDetectorTypePhoneNumber | Detect strings formatted as phone numbers. |
UIDataDetectorTypeLink | Detect strings formatted as URLs. |
UIDataDetectorTypeNone | Do not detect any data. |
UIDataDetectorTypeAll | Detect all available types of data. This constant is recommended for detecting data. |
To programmatically detect phone numbers or links, create an instance of your view and set its dataDetectorTypes
property to one of the data types in Table 1. See Listing 1 for an example that scans for phone numbers, emails, and URLs in a text view.
Listing 1 Programmatically detecting phone number and links in a text view
- (void)viewDidLoad { self.textView.dataDetectorTypes = UIDataDetectorTypeAll; } |
Using Interface Builder
Follow these steps to detect phone numbers and links using Interface Builder 3.1.2 or later:
Open the Nib file containing your web view or text view in Interface Builder.
Select your view, then choose Tools > Attributes Inspector. The Attributes Inspector contains the
Detect Phone Numbers
andDetect Links
options, which respectively turn on phone numbers detection and links (email and URLs) detection.Check one of these options or both of them according to your needs as shown in Figure 2.
Reference
Document Revision History
Date | Notes |
---|---|
2009-10-27 | New document that describes how to detect phone numbers and links in an iPhone application. |
Copyright © 2009 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2009-10-27