samedi 27 décembre 2014

How to implement number comparison like in the native iPhone contacts app?

I want to compare two numbers, like in case, for e.g. the number is 987654. It can be saved as +91 987654 or 0987654. But while searching or comparing, the number exactly matches and shows properly.


Right now I am using this code to compare the exact number. How do I enhance it?



// Remove non numeric characters from the phone number
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];

NSPredicate *predicate = [NSPredicate predicateWithBlock: ^(id record, NSDictionary *bindings) {
ABMultiValueRef phoneNumbers = ABRecordCopyValue( (__bridge ABRecordRef)record, kABPersonPhoneProperty);
BOOL result = NO;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *contactPhoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
contactPhoneNumber = [[contactPhoneNumber componentsSeparatedByCharactersInSet: [[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
//Comparing the string are equal. phoneNumber is the number I am comparing to.
if ([contactPhoneNumber isEqualToString:phoneNumber]) {
result = YES;
break;
}
}
CFRelease(phoneNumbers);
return result;
}];


The above code results YES, only if the contactPhoneNumber and phoneNumber are exactly same. Not when 0987654 or +91 987654 with 987654.


The same thing works in WhatsApp number comparison too. Can anyone give any leads?




Aucun commentaire:

Enregistrer un commentaire