以编程方式在iPhone中向地址簿添加多个地址时出现问题

时间:2011-04-22 05:24:17

标签: iphone objective-c ios4 addressbook 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(addressBook,personRecord,NULL);

如何插入多个地址?

1 个答案:

答案 0 :(得分:0)

是的,我自己得到了解决方案

我需要添加以下行

ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);

if (immutableMultiEmail) 

{

    multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);

} 
else 

{

    multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}

用于为同一字段创建新引用。现在工作得很完美......