728x90
Contacts.framework 는 iOS9 부터 기존 ABAddressBook.framework 를 대체합니다.
샘플프로젝트는 깃허브에 올려놓았습니다.
https://github.com/minjoongkim/iOS9-Contacts.framework-AddressBook-Sample
1. 주소록 불러오기
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if( status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
{
NSLog(@"access denied");
}
else
{
//Create repository objects contacts
CNContactStore *contactStore = [[CNContactStore alloc] init];
//Select the contact you want to import the key attribute ( https://developer.apple.com/library/watchos/documentation/Contacts/Reference/CNContact_Class/index.html#//apple_ref/doc/constant_group/Metadata_Keys )
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. 피커뷰로 불러오기
// Create a new contact view
CNContactViewController *contactController = [CNContactViewController viewControllerForContact:contact];
contactController.delegate = self;
contactController.allowsEditing = YES;
contactController.allowsActions = YES;
// Display the view
[self.navigationController pushViewController:contactController animated:YES];
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:[CNLabeledValue labeledValueWithLabel: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 = [CNContactViewController viewControllerForContact:contact];
contactController.delegate = self;
contactController.allowsEditing = YES;
contactController.allowsActions = YES;
// Display the view
[self.navigationController pushViewController:contactController animated:YES];
}
728x90
'Developer > iOS' 카테고리의 다른 글
[iOS]Keychain 이용해서 데이터(UUID) 저장하기 - WrapperClass제공 (0) | 2016.05.02 |
---|---|
[iOS9] AVPlayerViewController로 비디오 재생하기. Video player (1) | 2016.04.07 |
[ios]iOS8 UIAlertController Sample (0) | 2016.01.06 |
[iOS]iOS9 Sportlight 검색 추가하기 (0) | 2016.01.05 |
[iOS]iOS9 Contacts 이용해 연락처, 주소록 가져오기 (0) | 2016.01.05 |