以编程方式将多个地址添加到 iPhone 中的地址簿时出现问题
我正在向地址簿添加多个地址,但是当我尝试插入多个地址时,例如首先我将插入工作地址,然后如果我插入家庭地址,代码将插入家庭地址并删除工作地址。
这是我的代码:
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);
如何插入多个地址?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我自己得到了解决方案,
我需要添加以下行
来为同一字段创建新的参考。现在工作完美...
Yes I got solutions by my self
I need to add following lines
For creating new reference for the same field. And now working perfect...