ios address book

对于系统的contact 联系簿的ViewController 如何使用,下面又一个官方的demo,大家可以拿去看看。

这个东西,找了一段时间。其实不是很难,配合自己写的数据模型,很容易就能做出好的功能。只不过UI这块就要差一点了。

下面是一个类的h和m文件,大家自己把他搭建好,app delegate自己搭下,加入到一个NavigationController中即可。不需要其他的controller,只需要在导入两个系统的frame。

对于一些 contact 联系簿的controller,这里可以调用,不需要再去自己设计出 一个 很复杂的 contact了(本人尝试写了3000+的ui逻辑还没有实现它的全部功能,如果有人写出来了,或者网上有代码,希望可以学习下:),在此感激不尽)


[csharp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #import <AddressBook/AddressBook.h>  
  3. #import <AddressBookUI/AddressBookUI.h>  
  4.   
  5. @interface QuickContactsViewController : UITableViewController < ABPeoplePickerNavigationControllerDelegate,  
  6.                                                                  ABPersonViewControllerDelegate,  
  7.                                                                  ABNewPersonViewControllerDelegate,  
  8.                                                                  ABUnknownPersonViewControllerDelegate>  
  9. {  
  10.     NSMutableArray *menuArray;  
  11. }  
  12. @property (nonatomic, retain) NSMutableArray *menuArray;  
  13.   
  14. -(void)showPeoplePickerController;  
  15. -(void)showPersonViewController;  
  16. -(void)showNewPersonViewController;  
  17. -(void)showUnknownPersonViewController;  
  18.   
  19. @end  

[csharp]  view plain copy
  1. #import "QuickContactsViewController.h"  
  2.   
  3. enum TableRowSelected   
  4. {  
  5.     kUIDisplayPickerRow = 0,  
  6.     kUICreateNewContactRow,  
  7.     kUIDisplayContactRow,  
  8.     kUIEditUnknownContactRow  
  9. };  
  10.   
  11.   
  12. // Height for the Edit Unknown Contact row  
  13. #define kUIEditUnknownContactRowHeight 81.0  
  14.   
  15. @implementation QuickContactsViewController  
  16. @synthesize menuArray;  
  17.  
  18. #pragma mark Load views  
  19. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  20. - (void)viewDidLoad   
  21. {  
  22.     [super viewDidLoad];  
  23.     // Load data from the plist file  
  24.     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];  
  25.     self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];  
  26. }  
  27.  
  28.  
  29. #pragma mark Unload views  
  30. - (void)viewDidUnload   
  31. {  
  32.     self.menuArray = nil;  
  33.     [super viewDidUnload];  
  34. }  
  35.  
  36.  
  37. #pragma mark Table view methods  
  38. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  39. {  
  40.     return [menuArray count];  
  41. }  
  42.   
  43.   
  44. // Customize the number of rows in the table view.  
  45. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  46. {  
  47.     return 1;  
  48. }  
  49. // Customize the appearance of table view cells.  
  50. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  51. {  
  52.     static NSString *CellIdentifier = @"Cell";  
  53.     UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  54.     if (aCell == nil)  
  55.     {  
  56.         // Make the Display Picker and Create New Contact rows look like buttons  
  57.         if (indexPath.section < 2)  
  58.         {  
  59.             aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  60.             aCell.textLabel.textAlignment = UITextAlignmentCenter;  
  61.         }  
  62.         else  
  63.         {  
  64.             aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];  
  65.             aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  66.             aCell.detailTextLabel.numberOfLines = 0;  
  67.             // Display descriptions for the Edit Unknown Contact and Display and Edit Contact rows   
  68.             aCell.detailTextLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:@"description"];  
  69.         }  
  70.     }  
  71.       
  72.     aCell.textLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:@"title"];  
  73.       
  74.     return aCell;  
  75. }  
  76.   
  77.   
  78. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
  79. {  
  80.     switch (indexPath.section)  
  81.     {  
  82.         case kUIDisplayPickerRow:  
  83.             [self showPeoplePickerController];  
  84.             break;  
  85.         case kUICreateNewContactRow:  
  86.             [self showNewPersonViewController];  
  87.             break;  
  88.         case kUIDisplayContactRow:  
  89.             [self showPersonViewController];  
  90.             break;  
  91.         case kUIEditUnknownContactRow:  
  92.             [self showUnknownPersonViewController];  
  93.             break;  
  94.         default:  
  95.             [self showPeoplePickerController];  
  96.             break;  
  97.     }     
  98. }  
  99.  
  100.  
  101. #pragma mark TableViewDelegate method  
  102. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  103. {  
  104.     // Change the height if Edit Unknown Contact is the row selected  
  105.     return (indexPath.section==kUIEditUnknownContactRow) ? kUIEditUnknownContactRowHeight : tableView.rowHeight;      
  106. }  
  107.  
  108.  
  109. #pragma mark Show all contacts  
  110. // Called when users tap "Display Picker" in the application. Displays a list of contacts and allows users to select a contact from that list.  
  111. // The application only shows the phone, email, and birthdate information of the selected contact.  
  112. -(void)showPeoplePickerController  
  113. {  
  114.     ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];  
  115.     picker.peoplePickerDelegate = self;  
  116.     // Display only a person's phone, email, and birthdate  
  117.     NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],   
  118.                                 [NSNumber numberWithInt:kABPersonEmailProperty],  
  119.                                 [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];  
  120.       
  121.       
  122.     picker.displayedProperties = displayedItems;  
  123.     // Show the picker   
  124.     [self presentModalViewController:picker animated:YES];  
  125.     [picker release];     
  126. }  
  127.  
  128.  
  129. #pragma mark Display and edit a person  
  130. // Called when users tap "Display and Edit Contact" in the application. Searches for a contact named "Appleseed" in   
  131. // in the address book. Displays and allows editing of all information associated with that contact if  
  132. // the search is successful. Shows an alert, otherwise.  
  133. -(void)showPersonViewController  
  134. {  
  135.     // Fetch the address book   
  136.     ABAddressBookRef addressBook = ABAddressBookCreate();  
  137.     // Search for the person named "Appleseed" in the address book  
  138.     NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR("1"));  
  139.     // Display "Appleseed" information if found in the address book   
  140.     if ((people != nil) && [people count])  
  141.     {  
  142.         ABRecordRef person = (ABRecordRef)[people objectAtIndex:0];  
  143.         ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];  
  144.         picker.personViewDelegate = self;  
  145.         picker.displayedPerson = person;  
  146.         // Allow users to edit the person’s information  
  147.         picker.allowsEditing = YES;  
  148.         [self.navigationController pushViewController:picker animated:YES];  
  149.     }  
  150.     else   
  151.     {  
  152.         // Show an alert if "Appleseed" is not in Contacts  
  153.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"   
  154.                                                         message:@"Could not find Appleseed in the Contacts application"   
  155.                                                        delegate:nil   
  156.                                               cancelButtonTitle:@"Cancel"   
  157.                                               otherButtonTitles:nil];  
  158.         [alert show];  
  159.         [alert release];  
  160.     }  
  161.       
  162.     [people release];  
  163.     CFRelease(addressBook);  
  164. }  
  165.  
  166.  
  167. #pragma mark Create a new person  
  168. // Called when users tap "Create New Contact" in the application. Allows users to create a new contact.  
  169. -(void)showNewPersonViewController  
  170. {  
  171.     ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];  
  172.     picker.newPersonViewDelegate = self;  
  173.       
  174.     UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];  
  175.     [self presentModalViewController:navigation animated:YES];  
  176.       
  177.     [picker release];  
  178.     [navigation release];     
  179. }  
  180.  
  181.  
  182. #pragma mark Add data to an existing person  
  183. // Called when users tap "Edit Unknown Contact" in the application.   
  184. -(void)showUnknownPersonViewController  
  185. {  
  186.     ABRecordRef aContact = ABPersonCreate();  
  187.     CFErrorRef anError = NULL;  
  188.     ABMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);  
  189.     bool didAdd = ABMultiValueAddValueAndLabel(email, @"John-Appleseed@mac.com", kABOtherLabel, NULL);  
  190.       
  191.     if (didAdd == YES)  
  192.     {  
  193.         ABRecordSetValue(aContact, kABPersonEmailProperty, email, &anError);  
  194.         if (anError == NULL)  
  195.         {  
  196.             ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];  
  197.             picker.unknownPersonViewDelegate = self;  
  198.             picker.displayedPerson = aContact;  
  199.             picker.allowsAddingToAddressBook = YES;  
  200.             picker.allowsActions = YES;  
  201.             picker.alternateName = @"John Appleseed";  
  202.             picker.title = @"John Appleseed";  
  203.             picker.message = @"Company, Inc";  
  204.               
  205.             [self.navigationController pushViewController:picker animated:YES];  
  206.             [picker release];  
  207.         }  
  208.         else   
  209.         {  
  210.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"   
  211.                                                             message:@"Could not create unknown user"   
  212.                                                            delegate:nil   
  213.                                                   cancelButtonTitle:@"Cancel"  
  214.                                                   otherButtonTitles:nil];  
  215.             [alert show];  
  216.             [alert release];  
  217.         }  
  218.     }     
  219.     CFRelease(email);  
  220.     CFRelease(aContact);  
  221. }  
  222.  
  223.  
  224. #pragma mark ABPeoplePickerNavigationControllerDelegate methods  
  225. // Displays the information of a selected person  
  226. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person  
  227. {  
  228.     return YES;  
  229. }  
  230.   
  231.   
  232. // Does not allow users to perform default actions such as dialing a phone number, when they select a person property.  
  233. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person   
  234.                                 property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier  
  235. {  
  236.     return NO;  
  237. }  
  238.   
  239.   
  240. // Dismisses the people picker and shows the application when users tap Cancel.   
  241. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;  
  242. {  
  243.     [self dismissModalViewControllerAnimated:YES];  
  244. }  
  245.  
  246.  
  247. #pragma mark ABPersonViewControllerDelegate methods  
  248. // Does not allow users to perform default actions such as dialing a phone number, when they select a contact property.  
  249. - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person   
  250.                     property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue  
  251. {  
  252.     return NO;  
  253. }  
  254.  
  255.  
  256. #pragma mark ABNewPersonViewControllerDelegate methods  
  257. // Dismisses the new-person view controller.   
  258. - (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person  
  259. {  
  260.     [self dismissModalViewControllerAnimated:YES];  
  261. }  
  262.  
  263.  
  264. #pragma mark ABUnknownPersonViewControllerDelegate methods  
  265. // Dismisses the picker when users are done creating a contact or adding the displayed person properties to an existing contact.   
  266. - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person  
  267. {  
  268.     [self dismissModalViewControllerAnimated:YES];  
  269. }  
  270.   
  271.   
  272. // Does not allow users to perform default actions such as emailing a contact, when they select a contact property.  
  273. - (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person   
  274.                            property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier  
  275. {  
  276.     return NO;  
  277. }  
  278.  
  279.  
  280. #pragma mark Memory management  
  281. - (void)dealloc   
  282. {  
  283.     [menuArray release];  
  284.     [super dealloc];  
  285. }  
  286.   
  287. @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值