Navnath Memane
Navnath Memane

Reputation: 273

How to call from application to phone number

I am displaying Business card view on which I have one button on which I am setting Button title as phone number string which I got after parsing. I am getting perfect phone number string value on the button title. Now by pressing that button I want to call the default phone app so that User can call.

 -(void) BcardDisp: (id)sender
   {
BGView.hidden = NO;
if(BcardView.hidden == YES)
{

BcardView.hidden = NO;

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    marker *aMarker = (marker *)[appDelegate.markers objectAtIndex:selectedIndexPath.row];
for (int selectedIndexPath = 0; selectedIndexPath < [appDelegate.markers count]; selectedIndexPath++)
{

ShowroomName.text = aMarker.name;
    Address_Bcard.numberOfLines=3;
    Address_Bcard.text =aMarker.address;

    [p_Bcard setTitle:[NSString stringWithFormat:@"%@",aMarker.phone]  forState:UIControlStateNormal];
}
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}

}

For Calling number I am using following action on Button.

 - (IBAction)callfrom_BcardVeiw
  {
marker *aMarker = [[marker alloc] init];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"%d",aMarker.phone]];

[[UIApplication sharedApplication] openURL:phoneNumberURL];
NSLog(@"%d",phoneNumberURL);

  } 

But I am not able to call.....getting grabage value. What logic should I put under my - (IBAction)callfrom_BcardVeiw ....so that I can call on the same number as Button title string.

Upvotes: 1

Views: 538

Answers (3)

rckoenes
rckoenes

Reputation: 69469

Remove any space, or other charters that are not needed:

NSString *phoneNumber = [aMarker.phone stringByReplacingOccurrencesOfString:@" " withString:@""];

NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];

if ([[UIApplication sharedApplication] canOpenURL:phoneNumberURL ]) {
   [[UIApplication sharedApplication] openURL:phoneNumberURL];
} else {
   // iPad or iPod Touch, can't make a call
}

Upvotes: 0

gfdgfd
gfdgfd

Reputation: 104

Try this:

NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",aMarker.phone]];

Upvotes: 1

AliSoftware
AliSoftware

Reputation: 32681

Prefix the phone URL with "tel://".

Be careful too, Is aMarker.phone an integer or a string?

In one part of your code you use "%d" so it assumes it is an integer, and in the other part of your code you use "%@", which assumes it is a NSObject, probably an NSString.

I bet the phone number is a string (as it can contains characters others than digits, namely "+" for intl prefixes, or "*" or "#" for some services...) so you need to use @"tel://%@" and not @"tel://%d" or it will format strange values in your URL (the address of the string in memory, to be precise) instead of the actual phone number.

On the other hand, if the phone number is an integer (would be strange but your use of "%d" makes me perplex) and you use "%@", your code will crash, trying to access description method on some integer value that is not an NSObject (it will consider the integer as an address of an NSObject in memory whereas it is not, which will explain the BAD_ACCESS)

Upvotes: 0

Related Questions