Question

I am fetching a String from Parse.com into a UILabel programmatically in my app. The String is a phone number. So my question is, is it possible to give an action to the UILabel on click to make a Call.. or do I have to fetch the data from a Button, or better yet, is this possible?

If it is possible, how would should I go about? Does anyone have an example or tutorial I could follow? Any help would be appreciated. Thanks!

Was it helpful?

Solution

You question is in two part :

  1. Perform any kind on action when a UILabel is taped
  2. Perform a phone call action (action used in the first part)

First, to perform an action when a label is taped, you have to add a tap gesture recognizer to this label :

phoneNumberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = 
   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap)];
[phoneNumberLabel addGestureRecognizer:tapGesture];

Then you have to implement your phoneNumberLabelTap method :

 -(void)phoneNumberLabelTap
{
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phoneNumberLabel.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}

OTHER TIPS

In swift 3.0

   mobileLabel.isUserInteractionEnabled = true;
    let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))

    tap.delegate = self
    mobileLabel.addGestureRecognizer(tap)


func phoneNumberLabelTap()

{

    let phoneUrl = URL(string: "telprompt:\(mobileLabel.text ?? "")")!

    if(UIApplication.shared.canOpenURL(phoneUrl)) {

        UIApplication.shared.openURL(phoneUrl)
    }
    else {

        Constants.ShowAlertView(title: "Alert", message: "Cannot place call", viewController: self, isFailed: false)

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top