2009-11-10 126 views

回答

17

這是一個完整的如何通過創建ABRecordRef並使用視圖控制器將其推入視圖來顯示人員的工作示例

///////////////////////////// 將其掛鉤到自定義操作。

-(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, @"[email protected]", 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

謝謝,這是一個很大的幫助。 – 2010-02-14 17:27:09

+0

如果你想添加phonenumbers等irc它非常類似於添加郵件。只需查看屬性;) – hfossli 2010-02-15 10:31:23

+0

添加地址後,您還需要釋放addressDict。 – 2011-01-25 23:16:29

0

從目測來看,我想,而不是

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType); 

你想

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType); 
+0

您指出我在正確的方向。它應該是kABMultiDictionaryPropertyType。 – hfossli 2009-11-11 10:47:12

相關問題