在iOS应用程序中拨打电话 [英] Making a phone call in an iOS application

查看:94
本文介绍了在iOS应用程序中拨打电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码尝试在应用程序中进行调用,但它似乎没有工作:

I have some code which attempts to make a call within an application, but it doesn't seem to be working:

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

有时,变量 phone 就是这样的as @(102)222-2222。如何使用这样的电话号码拨打电话?我是否需要手动提取数字并删除所有额外的标点符号?

Sometimes, the variable phone is something such as @"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?

推荐答案

是的。你需要自己拿出来。或者您可以使用下面的代码段...

Yup. You need to take those out yourself. Or you can use the snippet below...

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

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

Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.

这篇关于在iOS应用程序中拨打电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆