diff --git a/SignalServiceKit/src/Contacts/Contact.h b/SignalServiceKit/src/Contacts/Contact.h index 72b5a31f8..6e064dd7f 100644 --- a/SignalServiceKit/src/Contacts/Contact.h +++ b/SignalServiceKit/src/Contacts/Contact.h @@ -49,6 +49,7 @@ NS_ASSUME_NONNULL_BEGIN + (NSComparator)comparatorSortingNamesByFirstThenLast:(BOOL)firstNameOrdering; + (NSString *)formattedFullNameWithCNContact:(CNContact *)cnContact NS_SWIFT_NAME(formattedFullName(cnContact:)); ++ (nullable NSString *)localizedStringForCNLabel:(nullable NSString *)cnLabel; - (CNContact *)buildCNContactMergedWithNewContact:(CNContact *)newCNContact NS_SWIFT_NAME(buildCNContact(mergedWithNewContact:)); diff --git a/SignalServiceKit/src/Contacts/Contact.m b/SignalServiceKit/src/Contacts/Contact.m index d2c76272d..0f6e45621 100644 --- a/SignalServiceKit/src/Contacts/Contact.m +++ b/SignalServiceKit/src/Contacts/Contact.m @@ -395,6 +395,27 @@ NS_ASSUME_NONNULL_BEGIN return [mergedCNContact copy]; } ++ (nullable NSString *)localizedStringForCNLabel:(nullable NSString *)cnLabel +{ + if (cnLabel.length == 0) { + return nil; + } + + NSString *_Nullable localizedLabel = [CNLabeledValue localizedStringForLabel:cnLabel]; + + // Docs for localizedStringForLabel say it returns: + // > The localized string if a Contacts framework defined label, otherwise just returns the label. + // But in practice, at least on iOS11, if the label is not one of CNContacts known labels (like CNLabelHome) + // kUnlocalizedStringLabel is returned, rather than the unadultered label. + NSString *const kUnlocalizedStringLabel = @"__ABUNLOCALIZEDSTRING"; + + if ([localizedLabel isEqual:kUnlocalizedStringLabel]) { + return cnLabel; + } + + return localizedLabel; +} + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Messages/Interactions/OWSContact.m b/SignalServiceKit/src/Messages/Interactions/OWSContact.m index ac1b088e5..adfefa277 100644 --- a/SignalServiceKit/src/Messages/Interactions/OWSContact.m +++ b/SignalServiceKit/src/Messages/Interactions/OWSContact.m @@ -610,9 +610,7 @@ NSString *NSStringForContactAddressType(OWSContactAddressType value) phoneNumber.phoneType = OWSContactPhoneType_Mobile; } else { phoneNumber.phoneType = OWSContactPhoneType_Custom; - if (phoneNumberField.label) { - phoneNumber.label = [CNLabeledValue localizedStringForLabel:phoneNumberField.label]; - } + phoneNumber.label = [Contact localizedStringForCNLabel:phoneNumberField.label]; } [phoneNumbers addObject:phoneNumber]; } @@ -628,9 +626,7 @@ NSString *NSStringForContactAddressType(OWSContactAddressType value) email.emailType = OWSContactEmailType_Work; } else { email.emailType = OWSContactEmailType_Custom; - if (emailField.label) { - email.label = [CNLabeledValue localizedStringForLabel:emailField.label]; - } + email.label = [Contact localizedStringForCNLabel:emailField.label]; } [emails addObject:email]; } @@ -656,9 +652,7 @@ NSString *NSStringForContactAddressType(OWSContactAddressType value) address.addressType = OWSContactAddressType_Work; } else { address.addressType = OWSContactAddressType_Custom; - if (addressField.label) { - address.label = [CNLabeledValue localizedStringForLabel:addressField.label]; - } + address.label = [Contact localizedStringForCNLabel:addressField.label]; } [addresses addObject:address]; }