iPhone:更新联系人的所有地址(家庭、工作、其他)
我正在以编程方式创建一个新联系人。除了地址之外,它工作得很好。以下是添加联系人的代码
ABAddressBookRef libroDirec = ABAddressBookCreate();
ABRecordRef persona = ABPersonCreate();
ABRecordSetValue(persona, kABPersonFirstNameProperty, tempSingle.firstName , nil);
ABRecordSetValue(persona, kABPersonLastNameProperty, tempSingle.lastName, nil);
ABRecordSetValue(persona, kABPersonMiddleNameProperty, tempSingle.middleName, nil);
if([tempSingle.homeStreet1 length]>0 || [tempSingle.homeStreet2 length]>0 || [tempSingle.homeCity length]>0 || [tempSingle.homeState length]>0 || [tempSingle.homePostal length]>0 || [tempSingle.homeCountry length]>0 )
{
ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
NSString *homeStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.homeStreet1,tempSingle.homeStreet2];
[addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary setObject:tempSingle.homeCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:tempSingle.homeState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:tempSingle.homePostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary setObject:tempSingle.homeCountry forKey:(NSString *)kABPersonAddressCountryKey];
//[addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryCodeKey];
bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);
if(didAddHome)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL);
}
[addressDictionary release];
}
if([tempSingle.workStreet1 length]>0 || [tempSingle.workStreet2 length]>0 || [tempSingle.workCity length]>0 || [tempSingle.workState length]>0 || [tempSingle.workPostal length]>0 || [tempSingle.workCountry length]>0 )
{
ABMutableMultiValueRef multiWork = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
NSString *workStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.workStreet1,tempSingle.workStreet2];
[addressDictionary1 setObject:workStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary1 setObject:tempSingle.workCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary1 setObject:tempSingle.workState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary1 setObject:tempSingle.workPostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary1 setObject:tempSingle.workCountry forKey:(NSString *)kABPersonAddressCountryKey];
bool didAddWork = ABMultiValueAddValueAndLabel(multiWork, addressDictionary1, kABWorkLabel, NULL);
if(didAddWork)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiWork, NULL);
}
[addressDictionary1 release];
}
if([tempSingle.otherStreet1 length]>0 || [tempSingle.otherStreet2 length]>0 || [tempSingle.otherCity length]>0 || [tempSingle.otherState length]>0 || [tempSingle.otherPostal length]>0 || [tempSingle.otherCountry length]>0 )
{
ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
NSString *otherStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.otherStreet1,tempSingle.otherStreet2];
[addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary2 setObject:tempSingle.otherCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary2 setObject:tempSingle.otherState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary2 setObject:tempSingle.otherPostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary2 setObject:tempSingle.otherCountry forKey:(NSString *)kABPersonAddressCountryKey];
bool didAddOther = ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABOtherLabel, NULL);
if(didAddOther)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiOther, NULL);
}
[addressDictionary2 release];
}
ABAddressBookAddRecord(libroDirec, persona, nil);
CFRelease(persona);
ABAddressBookSave(libroDirec, nil);
CFRelease(libroDirec);
如果我仅保存家庭地址或仅保存工作地址或仅保存其他地址,则代码运行良好。但如果我保存所有地址(家庭、工作和其他),则仅将最后一个地址保存到联系人中。请建议我如何解决此错误 请提出什么问题?
i am creating a new contact programmatically. it work well except address. following is the code to add a contact
ABAddressBookRef libroDirec = ABAddressBookCreate();
ABRecordRef persona = ABPersonCreate();
ABRecordSetValue(persona, kABPersonFirstNameProperty, tempSingle.firstName , nil);
ABRecordSetValue(persona, kABPersonLastNameProperty, tempSingle.lastName, nil);
ABRecordSetValue(persona, kABPersonMiddleNameProperty, tempSingle.middleName, nil);
if([tempSingle.homeStreet1 length]>0 || [tempSingle.homeStreet2 length]>0 || [tempSingle.homeCity length]>0 || [tempSingle.homeState length]>0 || [tempSingle.homePostal length]>0 || [tempSingle.homeCountry length]>0 )
{
ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
NSString *homeStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.homeStreet1,tempSingle.homeStreet2];
[addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary setObject:tempSingle.homeCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:tempSingle.homeState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:tempSingle.homePostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary setObject:tempSingle.homeCountry forKey:(NSString *)kABPersonAddressCountryKey];
//[addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryCodeKey];
bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);
if(didAddHome)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL);
}
[addressDictionary release];
}
if([tempSingle.workStreet1 length]>0 || [tempSingle.workStreet2 length]>0 || [tempSingle.workCity length]>0 || [tempSingle.workState length]>0 || [tempSingle.workPostal length]>0 || [tempSingle.workCountry length]>0 )
{
ABMutableMultiValueRef multiWork = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
NSString *workStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.workStreet1,tempSingle.workStreet2];
[addressDictionary1 setObject:workStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary1 setObject:tempSingle.workCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary1 setObject:tempSingle.workState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary1 setObject:tempSingle.workPostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary1 setObject:tempSingle.workCountry forKey:(NSString *)kABPersonAddressCountryKey];
bool didAddWork = ABMultiValueAddValueAndLabel(multiWork, addressDictionary1, kABWorkLabel, NULL);
if(didAddWork)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiWork, NULL);
}
[addressDictionary1 release];
}
if([tempSingle.otherStreet1 length]>0 || [tempSingle.otherStreet2 length]>0 || [tempSingle.otherCity length]>0 || [tempSingle.otherState length]>0 || [tempSingle.otherPostal length]>0 || [tempSingle.otherCountry length]>0 )
{
ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
NSString *otherStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.otherStreet1,tempSingle.otherStreet2];
[addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
[addressDictionary2 setObject:tempSingle.otherCity forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary2 setObject:tempSingle.otherState forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary2 setObject:tempSingle.otherPostal forKey:(NSString *)kABPersonAddressZIPKey];
[addressDictionary2 setObject:tempSingle.otherCountry forKey:(NSString *)kABPersonAddressCountryKey];
bool didAddOther = ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABOtherLabel, NULL);
if(didAddOther)
{
ABRecordSetValue(persona, kABPersonAddressProperty, multiOther, NULL);
}
[addressDictionary2 release];
}
ABAddressBookAddRecord(libroDirec, persona, nil);
CFRelease(persona);
ABAddressBookSave(libroDirec, nil);
CFRelease(libroDirec);
If i save only home address or only work address or only other address, then code works well. but if i save all address(Home, Work and Other) then only last address save to contacts.Please suggest how can i resolve this error
please suggest what is the wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由 kABPersonAddressProperty 标识的属性只能采用一个值,该值的类型为 ABMutableMultiValueRef,并且该多值可以包含多个带有某个标签的字典。
你可能需要的是这个..
the property identified by kABPersonAddressProperty can take just one value, which is of type ABMutableMultiValueRef, and this multivalue can contain many dictionaries in it with some label.
What you might need is probably this..