0

I have done call programmatically like this.

NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 

NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];

but I can't do call from iphone. What is the problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jinal
  • 69
  • 7

4 Answers4

2

now just replace this code with your code , may be its occurred because in phoneno or phone url somewhere whitespace is used so its not called..use this code..

    NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"];
    NSArray* telComponents = [phoneNumber componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    phoneNumber = [telComponents componentsJoinedByString: @""];

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

    if ( [[UIApplication sharedApplication] canOpenURL: telURL] )
    {
        [[UIApplication sharedApplication] openURL: telURL];
    }
    else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString( @"Dialer Error", @"" ) 
                                                        message: [NSString stringWithFormat: NSLocalizedString( @"There was a problem dialing %@.", @"" ), phoneNumber] 
                                                       delegate: nil 
                                              cancelButtonTitle: NSLocalizedString( @"OK", @"" ) 
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }

UPDATE:

try this also...

#import "RegexKitLite.h"

NSString * number = @"(555) 555-555 Office";
NSString * strippedNumber = [number stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];

see the link for this code from iphone sdk - Remove all characters except for numbers 0-9 from a string

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • it show error like:[__NSCFNumber componentsSeparatedByCharactersInSet:]: unrecognized selector sent to instance 0x1f831cd0' *** First throw call stack: – jinal Jan 04 '13 at 10:17
  • here NSArray* telComponents = [phoneNumber componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; – jinal Jan 04 '13 at 10:23
  • bcoz mobile number don't have space. – jinal Jan 04 '13 at 10:24
  • @jinal see this link for that also http://stackoverflow.com/questions/1160403/iphone-sdk-remove-all-characters-except-for-numbers-0-9-from-a-string – Paras Joshi Jan 04 '13 at 10:29
  • and @jinal also try my first code but first check that phoneNumber variable is not nil just add this line NSLog(@"\n\nPhone No. ==>> %@", phoneNumber); after Create that string variable and after use my code... – Paras Joshi Jan 04 '13 at 10:46
1

Try this & check:

 NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 
    NSString *phoneURLString = [@"tel://" stringByAppendingString:phoneNumber];
    NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
    [[UIApplication sharedApplication] openURL:phoneURL];
Vishal
  • 8,246
  • 6
  • 37
  • 52
0

Please try my sample code from here,

http://yuvarajmanickam.wordpress.com/2012/03/03/make-a-call-from-iphone-app/

Please use your phone number istead of phoneNumber string. Please let me know if it is useful for you. I hope it will help you. Thanks.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
0

Try this:

UIWebView *callWebview = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 0, 0)];
[self.view addSubview:callWebview];
NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 
    NSString *phoneWithoutSpaces = [[NSString stringWithFormat:@"tel://%@", phoneNumber] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *telURL = [NSURL URLWithString:phoneWithoutSpaces];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

It is working for me.

shivam
  • 1,148
  • 1
  • 12
  • 28