在 iOS 应用程序中拨打电话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6323171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a phone call in an iOS application
提问by CodeGuy
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]];
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?
有时,变量phone
是诸如@"(102) 222-2222"
. 我怎样才能用这样的电话号码拨打电话?我是否需要手动从中提取数字并去掉所有额外的标点符号?
回答by Johan Kool
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]];
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.
注意:您可能很想使用-stringByTrimmingCharactersInSet:
,但它只会删除字符串开头和结尾的字符,而不是出现在中间的字符。
回答by D-eptdeveloper
To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:
要返回原始应用程序,您可以使用 telprompt:// 而不是 tel:// - tell 提示将首先提示用户,但当呼叫完成后,它将返回到您的应用程序:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
just an update on above answer.
只是对上述答案的更新。
回答by Trianna Brannon
Here's a simple method that can be used to make a call and return to the app after the call is finished.
这里有一个简单的方法,可以用来拨打电话并在通话结束后返回应用程序。
Add the following to your .m file
将以下内容添加到您的 .m 文件中
- (void) dialNumber:(NSString*) number{
number = [@"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
Then add the following code wherever you want to make the call from:
然后在您要从其拨打电话的任何位置添加以下代码:
[self dialNumber:@"5031234567"];