Проблема при программном добавлении нескольких адресов в адресную книгу в iPhone

#iphone #objective-c #ios4 #addressbook #abaddressbook

#iPhone #objective-c #ios4 #адресная книга #abaddressbook

Вопрос:

Я добавляю несколько адресов в свою адресную книгу, но когда я пытаюсь вставить более одного адреса, например, сначала я вставлю рабочий адрес, а затем, если я вставлю домашний адрес, код вставит домашний адрес и удалит рабочий адрес.

Вот мой код:

 NSArray *mainComponents = [line componentsSeparatedByString:@":"];
NSArray *components = [[mainComponents objectAtIndex:1] componentsSeparatedByString:@";"];

if ([line rangeOfString:@"Work"].location != NSNotFound) 
{
    NSLog(@"Work--------");
    ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    NSString *otherStreetAddress=[NSString stringWithFormat:@"%@",[components objectAtIndex:0] ];

    [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:2] forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:3] forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary2 setObject:[components objectAtIndex:4] forKey:(NSString *)kABPersonAddressStateKey];
    [addressDictionary2 setObject:[components objectAtIndex:5] forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary2 setObject:[components objectAtIndex:6] forKey:(NSString *)kABPersonAddressCountryKey];

    ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABWorkLabel, NULL);

    [addressDictionary2 release];

    ABRecordSetValue(personRecord, kABPersonAddressProperty,multiOther , NULL);// (personRecord, kABPersonAddressProperty, multiOther, NULL);

    CFRelease(multiOther);

        //ABAddressBookAddRecord(addressBook, personRecord, NULL);
}
else if ([line rangeOfString:@"HOME"].location != NSNotFound) 
{
    NSLog(@"Home0--------");

    ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    NSString *otherStreetAddress=[NSString stringWithFormat:@"%@",[components objectAtIndex:0] ];

    [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:2] forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:3] forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary2 setObject:[components objectAtIndex:4] forKey:(NSString *)kABPersonAddressStateKey];
    [addressDictionary2 setObject:[components objectAtIndex:5] forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary2 setObject:[components objectAtIndex:6] forKey:(NSString *)kABPersonAddressCountryKey];

    ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABHomeLabel, NULL);

    [addressDictionary2 release];

    ABRecordSetValue(personRecord, kABPersonAddressProperty,multiOther , NULL);// (personRecord, kABPersonAddressProperty, multiOther, NULL);

    CFRelease(multiOther);
}
  

ABAddressBookAddRecord (адресная книга, PersonRecord, NULL);

Как вставить более одного адреса?

Ответ №1:

Да, я нашел решения самостоятельно

Мне нужно добавить следующие строки

 ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);

if (immutableMultiEmail) 

{

    multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);

} 
else 

{

    multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}
  

Для создания новой ссылки для того же поля. И теперь работает идеально…