Maison > Questions et réponses > le corps du texte
请教个问题:后台返回的json数据,有字符串和数字组成的信息。如何获取其中的电话号码,让其能够点击并且进行拨打。
大家讲道理2017-04-17 17:52:39
Après avoir obtenu les données json, utilisez NSString pour accepter uniformément les chaînes et les nombres. Lors de leur détection, la longueur des numéros de téléphone mobile est généralement fixe. C'est tout. Enfin, après avoir obtenu cette sous-chaîne, ajoutez simplement tel://
.怪我咯2017-04-17 17:52:39
-(void)callPhone:(NSString*)phoneNumber{
NSString *phoneStr=[NSString stringWithFormat:@"tel://%@",phoneNumber];
NSURL *url=[NSURL URLWithString:phoneStr];
[[UIApplication sharedApplication] openURL:url];
}
NSString *string;
NSString *pattern;
pattern=@"\d*";
string=@"s100s110s111jdlfj18203092902lsd";
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSLog(@"%@",error);
[regex enumerateMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
if (NSMatchingReportProgress==flags) {
}else{
/**
* 系统内置方法
*/
if (NSTextCheckingTypePhoneNumber==result.resultType) {
NSLog(@"%@",[string substringWithRange:result.range]);
}
/**
* 长度为11位的数字串
*/
if (result.range.length==11) {
NSLog(@"%@",[string substringWithRange:result.range]);
}
}
}];