【问题标题】:can't dial call from programmatically in iphone无法在 iphone 中以编程方式拨打电话
【发布时间】:2013-01-03 10:14:32
【问题描述】:

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

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

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

但我不能从 iphone 拨打电话。有什么问题?

【问题讨论】:

  • 请 NSLog thr phoneURLString 并检查它是否正确

标签: iphone openurl


【解决方案1】:

现在只需用您的代码替换此代码,可能是因为在 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的代码链接

【讨论】:

  • 它显示如下错误:[__NSCFNumber componentsSeparatedByCharactersInSet:]: unrecognized selector sent to instance 0x1f831cd0' *** First throw call stack:
  • 这里 NSArray* telComponents = [phoneNumber componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
  • bcoz 手机号没有空间。
  • @jinal 也可以看到这个链接stackoverflow.com/questions/1160403/…
  • 和@jinal 也尝试了我的第一个代码,但首先检查 phoneNumber 变量不是 nil 只需添加这一行 NSLog(@"\n\nPhone No. ==>> %@", phoneNumber);在创建该字符串变量并使用我的代码之后...
【解决方案2】:

试试这个并检查:

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

【讨论】:

    【解决方案3】:

    请从这里尝试我的示例代码,

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

    请使用您的电话号码而不是 phoneNumber 字符串。请让我知道它是否对您有用。我希望它会帮助你。谢谢。

    【讨论】:

      【解决方案4】:

      试试这个:

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

      它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-24
        • 1970-01-01
        • 2014-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多