iOS 导入通讯录开发代码

时间:2024-02-16 12:46:38
(self)
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
₯㎕  18:04:31
代理 
ABNewPersonViewControllerDelegate


 -(void)pushToAddressBookWith:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber andUserPhoto:(UIImage *)userPhoto andJobtitle:(NSString *)jobTiele andCompany:(NSString *)Company
{
    //设置未知人员
    ABRecordRef recordRef=ABPersonCreate();
    //姓名
    ABRecordSetValue(recordRef, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);
    ABRecordSetValue(recordRef, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);
    //phone(手机)
    ABMultiValueRef multiValueRef=ABMultiValueCreateMutable(kABStringPropertyType);
    ABMultiValueAddValueAndLabel(multiValueRef, (__bridge CFStringRef)phoneNumber, kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(recordRef, kABPersonPhoneProperty, multiValueRef, NULL);
    //职位
    ABRecordSetValue(recordRef, kABPersonJobTitleProperty, (__bridge CFStringRef)jobTiele, NULL);
    //公司
    ABRecordSetValue(recordRef, kABPersonOrganizationProperty, (__bridge CFStringRef)Company, NULL);
    
    //头像
    
    NSData *imageData = UIImagePNGRepresentation(userPhoto);
    ABPersonSetImageData(recordRef, (__bridge CFDataRef)imageData, NULL);
    
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc]init];
    newPersonVC.newPersonViewDelegate = self;
    newPersonVC.displayedPerson = recordRef;
    
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:newPersonVC];
    
    [self presentViewController:naVC animated:YES completion:nil];
    
}
//代理方法
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
    if (person) {
        NSLog(@"success");
    }
    else
        NSLog(@"error");
    [self dismissViewControllerAnimated:YES completion:nil];
}