我在地址簿中添加多个地址,但是当我尝试插入多个地址时,首先我会插入工作地址然后如果我插入家庭地址,代码将插入家庭地址并删除工作地址。
这是我的代码:
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(addressBook,personRecord,NULL);
如何插入多个地址?
答案 0 :(得分:0)
是的,我自己得到了解决方案
我需要添加以下行
ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);
if (immutableMultiEmail)
{
multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);
}
else
{
multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}
用于为同一字段创建新引用。现在工作得很完美......