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!
You question is in two 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];
}
}