Search code examples
iphoneobjective-cios4addressbookabaddressbook

Issue while adding multiple addressed to addressbook in iPhone programmatically


I am adding Multiple addresses to my address book, but when I try to insert more then one address, like first I'll insert Work address and then If I insert the Home Address, the code will insert the Home address and removes the Work address.

Here is my code:

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);

How to insert more than one addresses?


Solution

  • Yes I got solutions by my self

    I need to add following lines

    ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    
    ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);
    
    if (immutableMultiEmail) 
    
    {
    
        multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);
    
    } 
    else 
    
    {
    
        multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    }
    

    For creating new reference for the same field. And now working perfect...