Как создать контакты в адресной книге в iPhone SDK?

Как создать контакты в адресной книге в iPhone SDK?

Ответ 1

ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
ABRecordRef person = ABPersonCreate(); // create a person

NSString *phone = @"0123456789"; // the phone number to add

//Phone number is a list of phone number, so create a multivalue    
ABMutableMultiValueRef phoneNumberMultiValue  = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); // first name of the new person
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); // his last name
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property
ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

ABRecordRef group = ABGroupCreate(); //create a group
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group name
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group


ABAddressBookSave(addressBook, nil); //save the record



CFRelease(person); // relase the ABRecordRef  variable

Ответ 2

-(void)addContactInContactBook:(Info *)objInfo:(BOOL)isInsert:(BOOL)isUpdate:(BOOL)isDelete
{

if (isInsert)
{
    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
    ABRecordRef person = ABPersonCreate(); // create a person

    CFErrorRef  anError = NULL;
    ABMutableMultiValueRef phoneNumberMultiValue =
    ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.cellPhone),kABPersonPhoneMobileLabel, NULL);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.phone),kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); // set the phone number property


    // Address      
    ABMutableMultiValueRef address =
    ABMultiValueCreateMutable(kABDictionaryPropertyType);
    // Set up keys and values for the dictionary.
    CFStringRef keys[5];
    CFStringRef values[5];
    keys[0] = kABPersonAddressStreetKey;
    keys[1] = kABPersonAddressCityKey;
    keys[2] = kABPersonAddressStateKey;
    keys[3] = kABPersonAddressZIPKey;
    keys[4] = kABPersonAddressCountryKey;

    CFStringRef ref1 = (__bridge_retained CFStringRef)objInfo.street;

    CFStringRef ref2 = (__bridge_retained CFStringRef)objInfo.city;

    CFStringRef ref3 = (__bridge_retained CFStringRef)objInfo.state;

    CFStringRef ref4 = (__bridge_retained CFStringRef)objInfo.zipCode;

    CFStringRef ref5 = (__bridge_retained CFStringRef)objInfo.country;

    values[0] = ref1;
    values[1] = ref2;
    values[2] = ref3;
    values[3] = ref4;
    values[4] = ref5;



    CFDictionaryRef dicref = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    ABMultiValueIdentifier identifier;
    ABMultiValueAddValueAndLabel(address, dicref, kABHomeLabel, &identifier);
    ABRecordSetValue(person, kABPersonAddressProperty, address,&anError);


    if (![objInfo.FName isEqualToString:@""])
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)objInfo.FName , nil); // first name of the new person
    if (![objInfo.LName isEqualToString:@""])
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(objInfo.LName), nil); // his last name
    //    ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)(txtEmail.text), nil);

    if (![objInfo.Email isEqualToString:@""])
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(objInfo.Email), (CFStringRef)@"Global", NULL);
        ABRecordSetValue(person, kABPersonURLProperty, emailMultiValue, nil);
    }
    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(objInfo.company), nil);
    //    CFRelease(emailMultiValue);

    ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(objInfo.note), nil);


    ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

    //    ABRecordRef group = ABGroupCreate(); //create a group
    //    ABRecordSetValue(group, kABGroupNameProperty,@"My Group", nil); // set group name
    //    ABGroupAddMember(group, person, nil); // add the person to the group
    //    ABAddressBookAddRecord(addressBook, group, nil); // add the group


    BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

    if (isSaved) 
    {
        NSInteger num = ABRecordGetRecordID(person);

        NSString *qry = [NSString stringWithFormat:@"update %@ set phonebookId=%d,isSyncPhonebook='TRUE' where id = %d",TABLE,num,objInfo.infoId];
        [appDel insertORUpdateInfo:qry];
    }

}
else if(isUpdate)
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, objInfo.phonebookId);
    if(person != NULL)
    {
        CFErrorRef  anError = NULL;
        ABMutableMultiValueRef phoneNumberMultiValue =
        ABMultiValueCreateMutable(kABPersonPhoneProperty);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.cellPhone),kABPersonPhoneMobileLabel, NULL);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.phone),kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); // set the phone number property


        // Address      
        ABMutableMultiValueRef address =
        ABMultiValueCreateMutable(kABDictionaryPropertyType);
        // Set up keys and values for the dictionary.
        CFStringRef keys[5];
        CFStringRef values[5];
        keys[0] = kABPersonAddressStreetKey;
        keys[1] = kABPersonAddressCityKey;
        keys[2] = kABPersonAddressStateKey;
        keys[3] = kABPersonAddressZIPKey;
        keys[4] = kABPersonAddressCountryKey;

        CFStringRef ref1 = (__bridge_retained CFStringRef)objInfo.street;

        CFStringRef ref2 = (__bridge_retained CFStringRef)objInfo.city;

        CFStringRef ref3 = (__bridge_retained CFStringRef)objInfo.state;

        CFStringRef ref4 = (__bridge_retained CFStringRef)objInfo.zipCode;

        CFStringRef ref5 = (__bridge_retained CFStringRef)objInfo.country;

        values[0] = ref1;
        values[1] = ref2;
        values[2] = ref3;
        values[3] = ref4;
        values[4] = ref5;



        CFDictionaryRef dicref = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

        ABMultiValueIdentifier identifier;
        ABMultiValueAddValueAndLabel(address, dicref, kABHomeLabel, &identifier);
        ABRecordSetValue(person, kABPersonAddressProperty, address,&anError);


        if (![objInfo.FName isEqualToString:@""])
            ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)objInfo.FName , nil); // first name of the new person
        if (![objInfo.LName isEqualToString:@""])
            ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(objInfo.LName), nil); // his last name
        //    ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)(txtEmail.text), nil);

        if (![objInfo.Email isEqualToString:@""])
        {
            ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(objInfo.Email), (CFStringRef)@"Global", NULL);
            ABRecordSetValue(person, kABPersonURLProperty, emailMultiValue, nil);
        }
        ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(objInfo.company), nil);
        //    CFRelease(emailMultiValue);

        ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(objInfo.note), nil);


        //        ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

        //    ABRecordRef group = ABGroupCreate(); //create a group
        //    ABRecordSetValue(group, kABGroupNameProperty,@"My Group", nil); // set group name
        //    ABGroupAddMember(group, person, nil); // add the person to the group
        //    ABAddressBookAddRecord(addressBook, group, nil); // add the group


        BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

        if (isSaved) 
        {
//                NSInteger num = ABRecordGetRecordID(person);

            NSString *qry = [NSString stringWithFormat:@"update %@ set isUpdatePhonebook='TRUE' where id = %d",TABLE,objInfo.infoId];
            [appDel insertORUpdateInfo:qry];
        }

    }
//        ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
//        ABRecordRef person = ABPersonCreate(); // create a person


}
else if(isDelete)
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, objInfo.phonebookId);
    if(person != NULL)
    {
        CFErrorRef  anError = NULL;
        ABAddressBookRemoveRecord(addressBook, person, &anError);
        BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

        if (isSaved) 
        {
//                NSInteger num = ABRecordGetRecordID(person);

            NSString *qry = [NSString stringWithFormat:@"update %@ set isDeletePhonebook='TRUE' where id = %d",TABLE,objInfo.infoId];
            [appDel insertORUpdateInfo:qry];
        }

    }
}

//    CFRelease(person); // relase the ABRecordRef  variable

}