在iphone objective-c中添加联系人到地址簿

时间:2009-11-10 17:56:39

标签: iphone objective-c addressbook

在地址簿中设置街道地址等的正确方法是什么,让用户将其保存在iphone上?

编辑:删除了特定的代码问题并使其更加通用

2 个答案:

答案 0 :(得分:17)

这是一个完整的工作示例,介绍如何通过创建ABRecordRef并使用viewcontroller将其推入视图来展示人

///////////////////////////// 将其连接到自定义操作。

-(IBAction)addToAddressbook:(id)sender{  
    ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
    unknownPersonViewController.displayedPerson = (ABRecordRef)[self buildContactDetails];
    unknownPersonViewController.allowsAddingToAddressBook = YES;
    [self.navigationController pushViewController:unknownPersonViewController animated:YES];
    [unknownPersonViewController release]; 
}

//////////////////////////// 这是构建ABrecordRef的人

- (ABRecordRef)buildContactDetails {
    NSLog(@"building contact details");
    ABRecordRef person = ABPersonCreate(); 
    CFErrorRef  error = NULL;  

    // firstname
    ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);

    // email
    ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(email, @"expert.in@computer.com", CFSTR("email"), NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
    CFRelease(email); 

    // Start of Address
    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);  
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    [addressDict setObject:@"The awesome road numba 1" forKey:(NSString *)kABPersonAddressStreetKey];   
    [addressDict setObject:@"0568" forKey:(NSString *)kABPersonAddressZIPKey];  
    [addressDict setObject:@"Oslo" forKey:(NSString *)kABPersonAddressCityKey]; 
    ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, address, &error); 
    [addressDict release];
    CFRelease(address); 
    // End of Address

    if (error != NULL) 
        NSLog(@"Error: %@", error);

    [(id)person autorelease];
    return person;
}

//////////////////////////// 在标题中连接:

请记住导入这些框架:

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h> 

设置代理

ABNewPersonViewControllerDelegate

并将其添加到界面

ABNewPersonViewController *newPersonController;

答案 1 :(得分:0)

从眼球看,我想而不是

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType);

你想要

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);