안녕하세요
주소록이 iOS9 부터 Contact.Framework로 대체됨에 따라 샘플이 필요해서 만들어봤는데,
제 글 아랫글에 황정수님이 주소록 불러오는 샘플이 있어서 참고하고,
전 그 외에 추가하기, 삭제하기 등 다른 기능들 정리한김에 공유해드립니다.
필요하신분들은 나중에 보고 쓰세요~
샘플프로젝트는 깃허브에 올려놓았습니다.
https://github.com/minjoongkim/iOS9-Contacts.framework-AddressBook-Sample
1. 주소록 불러오기
CNAuthorizationStatus status = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
if( status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
{
NSLog(@"access denied");
}
else
{
//Create repository objects contacts
CNContactStore *contactStore = [[CNContactStore alloc] init];
NSArray *keys = [[NSArray alloc]initWithObjects:CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, CNContactViewController.descriptorForRequiredKeys, nil];
// Create a request object
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
request.predicate = nil;
[contactStore enumerateContactsWithFetchRequest:request
error:nil
usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop)
{
// Contact one each function block is executed whenever you get
NSString *phoneNumber = @"";
if( contact.phoneNumbers)
phoneNumber = [[[contact.phoneNumbers firstObject] value] stringValue];
NSLog(@"phoneNumber = %@", phoneNumber);
NSLog(@"givenName = %@", contact.givenName);
NSLog(@"familyName = %@", contact.familyName);
NSLog(@"email = %@", contact.emailAddresses);
[contactList addObject:contact];
}];
[contactTableView reloadData];
}
2. 주소록 피커뷰로 불러오기
-(IBAction)loadContactPickerView{
// Create a new picker
CNContactPickerViewController *contactPicker = [[CNContactPickerViewControlleralloc] init];
// Select property to pick
[contactPicker setDisplayedPropertyKeys:[[NSArray alloc] initWithObjects:CNContactEmailAddressesKey, nil] ];
[contactPicker setPredicateForEnablingContact:[NSPredicatepredicateWithFormat:@"emailAddresses.@count > 0"]];
[contactPicker setPredicateForSelectionOfContact:[NSPredicatepredicateWithFormat:@"emailAddresses.@count == 1"]];
// Respond to selection
contactPicker.delegate = self;
// Display picker
[self presentViewController:contactPicker animated:YES completion:nil];
}
3. 주소록 추가하기
-(void)saveContact:(NSString*)familyName givenName:(NSString*)givenName phoneNumber:(NSString*)phoneNumber {
CNMutableContact *mutableContact = [[CNMutableContact alloc] init];
mutableContact.givenName = givenName;
mutableContact.familyName = familyName;
CNPhoneNumber * phone =[CNPhoneNumber phoneNumberWithStringValue:phoneNumber];
mutableContact.phoneNumbers = [[NSArray alloc] initWithObjects:[CNLabeledValuelabeledValueWithLabel:CNLabelPhoneNumberiPhone value:phone], nil];
CNContactStore *store = [[CNContactStore alloc] init];
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addContact:mutableContact toContainerWithIdentifier:store.defaultContainerIdentifier];
NSError *error;
if([store executeSaveRequest:saveRequest error:&error]) {
NSLog(@"save");
[self reloadContactList];
}else {
NSLog(@"save error");
}
}
4. 주소록 업데이트하기
-(void)updateContact:(CNContact*)contact memo:(NSString*)memo{
CNMutableContact *mutableContact = contact.mutableCopy;
mutableContact.note = memo;
CNContactStore *store = [[CNContactStore alloc] init];
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest updateContact:mutableContact];
NSError *error;
if([store executeSaveRequest:saveRequest error:&error]) {
NSLog(@"save");
}else {
NSLog(@"save error : %@", [error description]);
}
}
5. 주소록 삭제하기
-(void)deleteContact:(CNContact*)contact {
CNMutableContact *mutableContact = contact.mutableCopy;
CNContactStore *store = [[CNContactStore alloc] init];
CNSaveRequest *deleteRequest = [[CNSaveRequest alloc] init];
[deleteRequest deleteContact:mutableContact];
NSError *error;
if([store executeSaveRequest:deleteRequest error:&error]) {
NSLog(@"delete complete");
[self reloadContactList];
}else {
NSLog(@"delete error : %@", [error description]);
}
}
6. 주소록 상세보기
-(void)loadContactView:(CNContact*)contact {
// Create a new contact view
CNContactViewController *contactController = [CNContactViewControllerviewControllerForContact:contact];
contactController.delegate = self;
contactController.allowsEditing = YES;
contactController.allowsActions = YES;
// Display the view
[self.navigationController pushViewController:contactController animated:YES];
}