[英]Making a phone call in an iOS application
我有一些代碼試圖在應用程序中進行調用,但它似乎不起作用:
UIApplication *myApp = [UIApplication sharedApplication];
NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
NSLog(@"making call with %@",theCall);
[myApp openURL:[NSURL URLWithString:theCall]];
有時,可變phone
是諸如@"(102) 222-2222"
類的東西。 我怎樣才能用這樣的電話號碼撥打電話? 我是否需要手動從中提取數字並去掉所有額外的標點符號?
是的。 你需要自己把那些拿出來。 或者您可以使用下面的代碼段...
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", cleanedString]];
注意:您可能很想使用-stringByTrimmingCharactersInSet:
,但它只刪除字符串開頭和結尾的字符,而不是它們出現在中間的字符。
要 go 返回原始應用程序,您可以使用 telprompt:// 而不是 tel:// - tell 提示符將首先提示用戶,但是當呼叫完成時,它將 go 返回您的應用程序:
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
只是上述答案的更新。
這是一個簡單的方法,可以用來撥打電話並在通話結束后返回應用程序。
將以下內容添加到 your.m 文件
- (void) dialNumber:(NSString*) number{
number = [@"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
然后在您要撥打電話的任何地方添加以下代碼:
[self dialNumber:@"5031234567"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.