无法在iphone中以编程方式拨打电话

时间:2013-01-03 10:14:32

标签: iphone openurl

我已经像这样以编程方式完成了调用。

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

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

但是我无法通过iphone拨打电话。有什么问题?

4 个答案:

答案 0 :(得分:2)

现在只需用您的代码替换此代码,可能是因为在phoneno或phone url某处使用了空格,所以它不会被调用..使用此代码..

    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];
    }

<强>更新

试试这个......

#import "RegexKitLite.h"

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

iphone sdk - Remove all characters except for numbers 0-9 from a string

中查看此代码的链接

答案 1 :(得分:1)

试试这个&amp;检查:

 NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 
    NSString *phoneURLString = [@"tel://" stringByAppendingString:phoneNumber];
    NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
    [[UIApplication sharedApplication] openURL:phoneURL];

答案 2 :(得分:0)

请从此处试用我的示例代码,

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

请使用您的电话号码而不是phoneNumber字符串。如果它对您有用,请告诉我。我希望它会对你有所帮助。感谢。

答案 3 :(得分:0)

试试这个:

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]];

这对我有用。