问题
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?
回答1:
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
回答2:
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];
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/14136786/cant-dial-call-from-programmatically-in-iphone