【问题标题】:Making a phone call in an iOS application在 iOS 应用程序中拨打电话
【发布时间】:2011-06-12 16:47:10
【问题描述】:

我有一些代码试图在应用程序中进行调用,但它似乎不起作用:

    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
    NSLog(@"making call with %@",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];

有时,变量phone 类似于@"(102) 222-2222"。我怎样才能用这样的电话号码拨打电话?我是否需要手动从中提取数字并去掉所有额外的标点符号?

【问题讨论】:

    标签: ios objective-c phone-call


    【解决方案1】:

    是的。你需要自己把那些拿出来。或者你可以使用下面的sn-p...

    NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
    NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", cleanedString]];
    

    注意:您可能很想使用-stringByTrimmingCharactersInSet:,但它只会删除字符串开头和结尾的字符,而不是出现在中间的字符。

    【讨论】:

    • 根据文档,我认为您不需要 URL 中的 // 。 Apple's phone URL scheme
    • 你是对的。确实不需要,尽管 iOS 也不会绊倒它们。无论如何,答案已更新。
    • 如果由于余额不足或网络信号问题导致通话失败,是否有办法获得回调
    • @JohanKool 为什么不直接使用 -stringByReplacingOccurencesOfString:withString: 来删除连字符?
    • @EvanR 我的 sn-p 实际上并没有删除任何连字符,因为这不是导致 URL 无法打开的原因。它删除任何不允许的字符。当然有一种方法可以使用-stringByReplacingOccurencesOfString:withString: 实现类似的代码,但我怀疑它会像这样简洁。
    【解决方案2】:

    要返回原始应用程序,您可以使用 telprompt:// 而不是 tel:// - tell 提示符将首先提示用户,但当通话结束时,它将返回您的应用程序:

    NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    

    只是上述答案的更新。

    【讨论】:

    • 你是对的,但要注意 telprompt 不是官方的 url 方案。 Apple 可以在未来的 iOS 版本中删除此方案。
    • 那么如果我们不使用telprompt,如何回到应用程序。
    【解决方案3】:

    这里有一个简单的方法,可以用来拨打电话并在通话结束后返回应用程序。

    将以下内容添加到您的 .m 文件中

    - (void) dialNumber:(NSString*) number{
    number = [@"telprompt://" stringByAppendingString:number];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
    }
    

    然后在您想要拨打电话的任何地方添加以下代码:

    [self dialNumber:@"5031234567"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多