Contenu connexe Plus de Takaaki Tanaka (7) AddressBook to Contacts7. Reference
• What's New in iOS 9.0
• Introducing the Contacts Framework for
iOS and OS X
• Address Book Framework Reference for
iOS
• Contacts Framework Reference
• http://qiita.com/koogawa/items/
44ce2e1fb127b884d835
8. Agenda
• 1. 連絡先について
• 2. AddressBookとContactsについて
• 3. Privacyについて
• 4. Fetchの違いについて
• 5. 連絡先の編集の違いについて
22. What's New in iOS 9.0
• AddressBookフレームワークがiOS 9から
deprecatedに
• AddressBookUIの方もdeprecatedに
• 現時点ではiOS 9でも使えないこともない…
23. What's New in iOS 9.0
• iOS 9でContactsフレームワークが追加された
• iOS 9からしか使用できない
• iOS 8以下もサポートする場合は
AddressBookと共存させなければならない
29. Contacts
• iOS / Macの連絡先アクセスAPI
• watchOS 2に対応
• Objective-C / Swift両方使用可能
• thread-safe(fetch & save)
35. AddressBook
• 連絡先アクセスの許可をユーザーに問い合わせる
CFErrorRef cfError = NULL;
ABAddressBookRef addressBook =
ABAddressBookCreateWithOptions(NULL,
&cfError);
ABAddressBookRequestAccessWithCompletion(
addressBook, ^(bool inGranted,
CFErrorRef inCfError) {
NSLog(@"inGranted: %d", inGranted);
});
43. Contacts
• CNContact.identifierを使用して取得
• String型
• AddressBookのRecordIDと互換性がない
let identifier =
"035218FA-1E6E-4D1C-9708-76FBC0E55F28"
let contact = try
store.unifiedContactWithIdentifier(
identifier,
keysToFetch:[
CNContactGivenNameKey,
CNContactFamilyNameKey])
46. Contacts
• keysToFetchに指定していないプロパティ
へはアクセスできない
let predicate =
CNContact.predicateForContactsMatchingName(
"Appleseed")
let contacts = try
store.unifiedContactsMatchingPredicate(
predicate,
keysToFetch:[CNContactGivenNameKey,
CNContactFamilyNameKey])
let contact = contacts.first
print("(contact!.identifier) (contact!.givenName)
(contact!.familyName) (contact!.phoneNumbers)")
48. Contacts
• フルネームを取得する際KeysToFetchに全て指定しない
let contacts = try
store.unifiedContactsMatchingPredicate(
predicate,
keysToFetch:[
CNContactGivenNameKey,
CNContactFamilyNameKey,
CNContactNamePrefixKey,
CNContactMiddleNameKey,
CNContactNameSuffixKey,
CNContactPhoneNumbersKey])
54. AddressBook
• 新規追加の場合(全体)
CFErrorRef cfError = NULL;
ABAddressBookRef addressBook =
ABAddressBookCreateWithOptions(NULL, &cfError);
if (!addressBook) {
NSError *error = (__bridge_transfer NSError *)cfError;
NSLog(@"%s: %@", __PRETTY_FUNCTION__, error);
}
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonFirstNameProperty,
(__bridge CFTypeRef)@"John", &cfError);
ABRecordSetValue(person, kABPersonLastNameProperty,
(__bridge CFTypeRef)@"Appleseed", &cfError);
ABAddressBookAddRecord(addressBook, person, &cfError);
ABAddressBookSave(addressBook, &cfError);
56. AddressBook
• ABRecordRefを取得する
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person,
kABPersonFirstNameProperty,
(__bridge CFTypeRef)@"John", &cfError);
ABRecordSetValue(person,
kABPersonLastNameProperty,
(__bridge CFTypeRef)@"Appleseed", &cfError);
• 必要なプロパティをセットする
57. AddressBook
• MultiValueを追加する場合
CFErrorRef cfError = NULL;
ABMultiValueIdentifier identifier;
ABMultiValueRef multiTel =
ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFStringRef label1 = kABPersonPhoneMainLabel;
NSString *value1 = @"012-345-6789";
ABMultiValueAddValueAndLabel(
multiTel, (__bridge CFTypeRef)(value1), label1, &identifier);
CFStringRef label2 = kABPersonPhoneIPhoneLabel;
NSString *value2 = @"111-222-3333";
ABMultiValueAddValueAndLabel(
multiTel, (__bridge CFTypeRef)(value2), label2, &identifier);
ABRecordSetValue(
person, kABPersonPhoneProperty, multiTel, &cfError);
CFRelease(multiTel);
59. Contacts
• 新規追加の場合(全体)
let store = CNContactStore()
let contact = CNMutableContact()
contact.givenName = "John"
contact.familyName = "Appleseed"
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact, toContainerWithIdentifier:nil)
do {
try store.executeSaveRequest(saveRequest)
} catch {
abort()
}
62. Contacts
• MultiValueを追加する場合
let mainNumber =
CNLabeledValue(
label:CNLabelPhoneNumberMain,
value:"012-345-6789")
let iPhoneNumber =
CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:"111-222-3333")
contact.phoneNumbers = [mainNumber, iPhoneNumber]