我正在创建一个应用程序,我需要将联系人保存到地址簿中。一切正常,除非我添加kABPersonAddressProperty
,首先我添加它们然后我保存地址并在保存时崩溃。
我得到的错误是:
-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30'
以下是我正在使用的代码:
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
//
//some code here, not relevant
//
ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);
ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);
//More irrelevant code here
ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreateWithOptions(nil, NULL);
BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);
if(isAdded){
NSLog(@"added..");
}
if (error != NULL) {
NSLog(@"ABAddressBookAddRecord %@", error);
}
error = NULL;
BOOL isSaved = ABAddressBookSave (addressBook, &error);
每当我运行此代码时,错误始终为NULL,并且isAdded始终为true,但在执行ABAddressBookSave(addressBook,&error);
时仍然会崩溃
另一个重要的事情是,如果我删除这部分代码:
ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);
ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);
联系人添加正常,包括姓名,姓氏,多个电话号码,网址和电子邮件。
答案 0 :(得分:2)
地址属性不是kABMultiStringPropertyType
,而是kABMultiDictionaryPropertyType
。
要解决崩溃,请尝试将呼叫更改为ABMultiValueCreateMutable
,并将其传递给kABMultiDictionaryPropertyType
。
然后,您还需要通过从地址字符串值创建字典来更新填充地址信息的方式。查看this post上的示例。
它应该看起来像这样(未经测试):
ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
self.Street.text, (NSString *)kABPersonAddressStreetKey,
self.ZIPcode.text, (NSString *)kABPersonAddressZIPKey,
self.City.text, (NSString *)kABPersonAddressCityKey,
nil];
ABMultiValueAddValueAndLabel(addresses, (CFDictionaryRef)values, kABHomeLabel, NULL);
ABRecordSetValue(aRecord, kABPersonAddressProperty, addresses, &anError);