以编程方式将多个地址添加到 iPhone 中的地址簿时出现问题

发布于 2024-11-02 13:02:46 字数 3056 浏览 12 评论 0原文

我正在向地址簿添加多个地址,但是当我尝试插入多个地址时,例如首先我将插入工作地址,然后如果我插入家庭地址,代码将插入家庭地址并删除工作地址。

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-11-09 13:02:46

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

我需要添加以下行

ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);

if (immutableMultiEmail) 

{

    multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);

} 
else 

{

    multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}

来为同一字段创建新的参考。现在工作完美...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文