[英]Phone call number with hashtag on iOS
如何在iOS上撥打此號碼* 199 * 123456789#?
我使用了以下代碼,但是它不起作用。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:*199*123456789#"]];
從我對現在已關閉的問題“ iOS的答案重新發布並進行了修改,” 我想使用telprompt在Xcode中撥打電話號碼“#51234 ”:
至少從iOS 11開始,您可以撥打帶有井號(#)或星號(*)的號碼。
通過首先對電話號碼進行編碼 ,然后添加tel:
前綴,最后將結果字符串轉換為URL,使用這些字符進行通話。
Swift 4,iOS 11
// 1) set up the dial sequence as a String
let dialSequence = "*199*123456789#"
// 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
guard let encodedDialSequence =
dialSequence.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
print("Unable to encode the dial sequence.")
return
}
// 3) add the `tel:` url scheme to the front of the encoded string
// NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
let dialURLString = "tel:\(encodedDialSequence)"
// 4) set up the URL with the scheme+encoded number string
guard let dialURL = URL(string: dialURLString) else {
print("Couldn't convert the dial string into an URL.")
return
}
// 5) dial the URL
UIApplication.shared.open(dialURL, options: [:]) { success in
if success { print("SUCCESSFULLY OPENED DIAL URL") }
else { print("COULDN'T OPEN DIAL URL") }
}
iOS 11的Objective-C
// 1) set up the dial sequence as a String
NSString *dialSequence = @"*199*123456789#";
// 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
NSCharacterSet *urlHostAllowed = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *encodedDialSequence = [dialSequence stringByAddingPercentEncodingWithAllowedCharacters:urlHostAllowed];
// 3) add the 'tel:' url scheme to the front of the encoded string
// NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
NSString *dialURLString = [NSString stringWithFormat:@"tel:%@", encodedDialSequence];
// 4) set up the URL with the scheme+encoded number string
NSURL *dialURL = [NSURL URLWithString:dialURLString];
// 5) set up an empty dictionary for the options parameter
NSDictionary *optionsDict = [[NSDictionary alloc] init];
// 6) dial the URL
[[UIApplication sharedApplication] openURL:dialURL
options:optionsDict
completionHandler:^(BOOL success) {
if (success) { NSLog(@"SUCCESSFULLY OPENED DIAL URL"); }
else { NSLog(@"COULDN'T OPEN DIAL URL"); }
}];
將*
替換為%2A
,將#
替換為%23
:
NSURL *tel = [NSURL URLWithString:@"tel:%2A199%2A123456789%23"];
[[UIApplication sharedApplication] openURL:tel];
您需要使用tel://,而不僅僅是tel:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.