地址簿实现不起作用

时间:2011-11-17 14:10:01

标签: iphone objective-c ios addressbook

我正在使用XCode 4.2开发一个函数来向地址簿添加联系人,这是我的代码

 ABAddressBookRef *iPhoneAddressBook = ABAddressBookCreate();
 ABRecordRef contact = ABPersonCreate();

 //add infos
 ABRecordSetValue(contact, kABPersonFirstNameProperty,(__bridge_retained CFStringRef)firstName, nil);
 ABRecordSetValue(contact, kABPersonLastNameProperty,(__bridge_retained CFStringRef)lastName, nil);
 ABRecordSetValue(contact, kABPersonOrganizationProperty, (__bridge_retained CFStringRef)organization, nil);
 ABRecordSetValue(contact, kABPersonJobTitleProperty, (__bridge_retained CFStringRef)title, nil);


 ABMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiRealPropertyType);


 ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL);
 ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workFax, kABPersonPhoneWorkFAXLabel, NULL);

 ABRecordSetValue(contact, kABPersonPhoneProperty, multiPhone, nil);
 CFRelease(multiPhone);

 ABMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);

 ABMultiValueAddValueAndLabel(multiEmail, (__bridge_retained CFStringRef)email, kABWorkLabel, NULL);

 ABRecordSetValue(contact, kABPersonEmailProperty, multiEmail, nil);

 CFRelease(multiEmail);
// address

ABMultiValueRef multiAddress =ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc]init];
[addressDict setObject:address forKey:(NSString *) kABPersonAddressStreetKey];
[addressDict setObject:city forKey:(NSString *) kABPersonAddressCityKey];
[addressDict setObject:province forKey:(NSString *) kABPersonAddressStateKey];
[addressDict setObject:postalCode forKey:(NSString *) kABPersonAddressZIPKey];
[addressDict setObject:address forKey:(NSString *) kABPersonAddressCountryKey];

ABMultiValueAddValueAndLabel(multiAddress, (__bridge_retained CFStringRef)addressDict, kABWorkLabel, NULL);
ABRecordSetValue(contact, kABPersonAddressProperty, multiAddress, NULL);


CFRelease(multiAddress);

ABMultiValueRef multiURL =ABMultiValueCreateMutable(kABMultiRealPropertyType);
ABMultiValueAddValueAndLabel(multiURL, (__bridge_retained CFStringRef)link, kABPersonURLProperty, NULL);
CFRelease(multiURL);



 ABAddressBookAddRecord(iPhoneAddressBook, contact, nil);

 BOOL didAdd = ABAddressBookSave(iPhoneAddressBook, nil);

 CFRelease(contact);
 CFRelease(iPhoneAddressBook);

 //notifying the user that it was stored in his address book
 if (didAdd) {


 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" 
 message:@"Contact Info successfully added to the Address Book" 
 delegate:self 
 cancelButtonTitle:@"OK" 
 otherButtonTitles:nil];
 [alert show];
 }

程序编译,但它在此行停止:

ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL);

我收到此错误(绿色)

Thread 1

任何线索?代码有什么问题?

1 个答案:

答案 0 :(得分:0)

这是发生错误的函数的定义:

bool ABMultiValueAddValueAndLabel (
   ABMutableMultiValueRef multiValue,
   CFTypeRef value,
   CFStringRef label,
   ABMultiValueIdentifier *outIdentifier
);

看来你已经切换了第二和第三个参数,对吗?