如何通过Phonegap使用iOS格式查找联系人的电话号码

how to find contact's phone number with iOS formatting though Phonegap?

本文关键字:查找 联系人 电话号码 格式 iOS 何通过 Phonegap 使用      更新时间:2023-09-26

所以我在尝试使用 Cordova Phonegap 搜索联系人时遇到问题,以查找 IOS 设备上包含区号和/或国家/地区代码的电话号码。

例如:我与#1(718)555-5555有联系。当我显示该联系人信息(通过使用contacts.find()通过电话间隙)时,电话号码字符串显示为"1(718)555-5555"。但是如果我尝试使用"1(718)555-5555"进行搜索,Phonegap找不到它。

可以找到没有国家代码和区号的号码,例如"555-5555"。

iOS 中包含区号和/或国家/地区代码的电话号码究竟如何存储?

function findContact(num) {
if (!navigator.contacts) {
    console.log("Contacts API not supported");
    return;
}
var options = new ContactFindOptions();
options.filter = num;
options.multiple = true;
var fields = ["phoneNumbers"];
navigator.contacts.find(fields, function(contacts) {
                        if (contacts.length == 0) {
                        console.log('Contact not found, num ' + num);
                        }
                        else {
                        console.log('Found ' + contacts.length + ' contacts w/num: ' + num);
                        }
                        }, function(error){}, options);    
}

*编辑*所以我将问题范围缩小到科尔多瓦联系人插件中的 CDVContact.m。在测试中多值字符串方法

 valueArray = [self valuesForProperty:propId inRecord:self.record];  
if (valueArray) { 
NSString* valuesAsString = [valueArray componentsJoinedByString:@" "]; 
NSPredicate* containPred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", testValue]; 
bFound = [containPred evaluateWithObject:valuesAsString]; } 

valuesAsString 确实以 @"1 (555) 555-5555" 等格式正确显示,并且 testValue 字符串相同,但 bFound 的计算结果仍为 NO。

也许有一些与 valueArray 相关的元数据导致了错误?

所以我发现iOS电话号码中的空格是非阻塞空格,即&nbsp。必须使用 String.fromCharCode(160) 声明搜索字符串中的任何空格。