增加个人信息到通讯录

来源:互联网 发布:部门考核数据流程图 编辑:程序博客网 时间:2024/04/28 07:58
#pragma mark -
#pragma mark Add a person to Address Book
- (IBAction)addPersonButton:(id)sender {
    
    UITableViewCell *cell=(UITableViewCell *)[sender superview];
    
    ABRecordRef aRecord = ABPersonCreate();
    CFErrorRef  anError = NULL;
    
    // Username
    UILabel *label = (UILabel *)[cell viewWithTag:11];
    NSString *username = [[NSString alloc] initWithFormat:@"%@",label.text];
    NSLog(@"%@",label.text);
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, username, &anError);
    [username release];
    
    // Phone Number
    label = (UILabel *)[cell viewWithTag:12];
    NSLog(@"%@",label.text);
    NSString *usercontact = [[NSString alloc] initWithFormat:@"%@",label.text];
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (CFStringRef)usercontact, kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
    CFRelease(multi);
    [usercontact release];
    
    // Company
    label = (UILabel *)[cell viewWithTag:13];
    NSLog(@"%@",label.text);
    NSString *usercompany = [[NSString alloc] initWithFormat:@"%@",label.text];
    ABRecordSetValue(aRecord, kABPersonDepartmentProperty, usercompany, &anError);
    [usercompany release];
    
    
    // image
    UIImageView *imageView=(UIImageView*) [cell viewWithTag:10];
    NSData *dataref = UIImagePNGRepresentation(imageView.image);
    UIImage *defultImage =[UIImage imageNamed:@"pic_s_head.png"];
    NSData *defultImageData = UIImagePNGRepresentation(defultImage);
    if (dataref == nil) {
        dataref = UIImageJPEGRepresentation(imageView.image, 1);
    }
    if (![dataref isEqualToData:defultImageData]) {
        ABPersonSetImageData(aRecord, (CFDataRef) dataref , &anError);
    }
    
    if (anError != NULL)
        NSLog(@"error while creating..");
    
    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();
    
    BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);
    
    if(isAdded){
        
        NSLog(@"added..");
    }
    if (error != NULL) {
        NSLog(@"ABAddressBookAddRecord %@", error);
    }
    error = NULL;
    
    BOOL isSaved = ABAddressBookSave (addressBook, &error);
    
    if(isSaved) {
        
        NSLog(@"saved..");
        UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:@"Phone added successfully to your addressbook" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertOnChoose show];
        [alertOnChoose release];
    }
    
    if (error != NULL) {
        
        NSLog(@"ABAddressBookSave %@", error);
        UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:@"Unable to save this time" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertOnChoose show];
        [alertOnChoose release];
    }
    
    CFRelease(aRecord);
    
}