What are the Tags Around Default iPhone Address Book People Phone Number Labels?
My question concerns markup that surrounds some of the default phone number labels in the 开发者_如何转开发Person entries of the Contact list on the iPhone.
I have created an iPhone contact list address book entry for a person, "John Smith" with the following phone number entries:
- Mobile (604) 123-4567
- iPhone (778) 123-4567
- Home (604) 789-4561
- Work (604) 456-7891
- Main (604) 789-1234
- megaphone (234) 567-8990
Note that the first five labels are default labels provided by the Contacts application and the last label, "megaphone", is a custom label.
I wrote the following method to retrieve and display the labels and phone numbers for each person in the address book:
-(void)displayPhoneNumbersForAddressBook {
ABAddressBookRef book = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);
ABRecordRef record = CFArrayGetValueAtIndex(people, 0);
ABMultiValueRef multi = ABRecordCopyValue(record, kABPersonPhoneProperty);
NSLog(@"---------" );
NSLog(@"displayPhoneNumbersForAddressBook" );
CFStringRef label, phone;
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); ++i) {
label = ABMultiValueCopyLabelAtIndex(multi, i);
phone = ABMultiValueCopyValueAtIndex(multi, i);
NSLog(@"label: \"%@\" number: \"%@\"", (NSString*)label, (NSString*)phone);
CFRelease(label);
CFRelease(phone);
}
NSLog(@"---------" );
CFRelease(multi);
CFRelease(people);
CFRelease(book);
}
and here is the output for the address book entry that I entered:
2010-03-08 13:24:28.789 test2m[2479:207] ---------
2010-03-08 13:24:28.789 test2m[2479:207] displayPhoneNumbersForAddressBook
2010-03-08 13:24:28.790 test2m[2479:207] label: "_$!<Mobile>!$_" number: "(604) 123-4567"
2010-03-08 13:24:28.790 test2m[2479:207] label: "iPhone" number: "(778) 123-4567"
2010-03-08 13:24:28.791 test2m[2479:207] label: "_$!<Home>!$_" number: "(604) 789-4561"
2010-03-08 13:24:28.791 test2m[2479:207] label: "_$!<Work>!$_" number: "(604) 456-7891"
2010-03-08 13:24:28.792 test2m[2479:207] label: "_$!<Main>!$_" number: "(604) 789-1234"
2010-03-08 13:24:28.792 test2m[2479:207] label: "megaphone" number: "(234) 567-8990"
2010-03-08 13:24:28.793 test2m[2479:207] ---------
What are the markup characters
_$!< and >!$_
surrounding most, save for iPhone, of the default labels for?
Can you point me to where in the "Address Book Programming Guide for iPhone OS" I can find the information?
Thank you for your help.
I'm running into the same issue. This is what I think so far.
The markup that your seeing indicates to the system that this is a Default label and not a custom label. If you run this code:
NSLog(kABOtherLabel);
you'll ge this result:
_$!<Other>!$_
So that is the value stored in the kABOtherLabel
constant (of type CFStringRef). I think the reason iPhone
doesn't have the markup around it is becuase it's a 'Custom' label, but it's one originated by Apple instead of the user.
You can give the label any value you like, as evidenced by your megaphone
label above. But notice that if you try and create a phone number (or e-mail address) with the label 'other' without using the kABOtherLabel
constant or it's value _$!<Other>!$_
, the system will think that you're creating a custom label. Like in this example:
ABMultiValueAddValueAndLabel(email, @"nospam@notarealdomain.com", @"other", NULL);
And if you go and edit that Address Book entry on the iPhone, it will show up in a separate list of custom labels. (So there will be 2 choices for 'other', one in the defaults and one in the custom labels)
While this hasn't answered your question, I hope it helps.
This is largely the same in the new CNContact framework that replaced ABAddressBook as the recommended way to handle contacts.
There are six default labels that Apple provides, which are referenced with phone-specific CNLabelledValue constants:
CNLabelPhoneNumberiPhone = "iPhone"
CNLabelPhoneNumberMobile = "_$!<Mobile>!$_"
CNLabelPhoneNumberMain = "_$!<Main>!$_"
CNLabelPhoneNumberHomeFax = "_$!<HomeFAX>!$_"
CNLabelPhoneNumberOtherFax = "_$!<OtherFAX>!$_"
CNLabelPhoneNumberPager = "_$!<Pager>!$_"
The inclusion of _$!<
and >!$_
around five of these constants is most likely a marker that the strings can be localised by the operating system, given the availability of the method localizedString(forLabel:). I believe the reason these delimiters do not appear around iPhone is because Apple does not localize iPhone, which is displayed as "iPhone" in all languages.
The sensible behaviour would be to hide these when displaying the string, which occurs using localizedString(forLabel:)
, continue to store these delimiters with the string if editing the entry, and map any user-created label to these strings where the custom label was equal to the main body of these, i.e., Mobile, Main, Pager, and so on.
I'm working with iOS 14 and unfortunately this is still a problem when dealing with Apple's Contact API. My solution is not the best, but it works.
Swift 5
extension String {
func removeMarkUpAroundContactLabel() -> String{
var newString = self
for _ in 0..<4{
newString.removeFirst()
newString.removeLast()
}
return newString
}
}
精彩评论