标签:
iOS的地址簿技术提供一个在集中式数据库中存储用户联系人信息和其他私人信息、在应用程序间分享这些信息的方式。
#import <UIKit/UIKit.h> |
#import <AddressBookUI/AddressBookUI.h> |
@interface ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate> |
@property (weak, nonatomic) IBOutlet UILabel *firstName; |
@property (weak, nonatomic) IBOutlet UILabel *phoneNumber; |
- (IBAction)showPicker:(id)sender; |
@end |
- (IBAction)showPicker:(id)sender |
{ |
ABPeoplePickerNavigationController *picker = |
[[ABPeoplePickerNavigationController alloc] init]; |
picker.peoplePickerDelegate = self; |
[self presentModalViewController:picker animated:YES]; |
} |
这个picker在它自己的delegate里调用方法来相应用户的动作。
1.如果用户取消,则调用- (void)peoplePickerNavigationControllerDidCancel: 来dismiss这个picker
2.如果用户选择了地址簿里的一个人,则将调用
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
复制名字和号码放入标签中,然后再dismiss Picker
3.当用户点击picker中选中的人的一个属性,将调用
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
在这个应用程序中,当用户选中一个人时,picker总是会dismiss,所以没办法让用户选中这个人的一个属性。这意味着这个方法将不再被调用。但是如果把这个方法排除在外,整个协议的实现方法又不完整。
- (void)peoplePickerNavigationControllerDidCancel: |
(ABPeoplePickerNavigationController *)peoplePicker |
{ |
[self dismissModalViewControllerAnimated:YES]; |
} |
- (BOOL)peoplePickerNavigationController: |
(ABPeoplePickerNavigationController *)peoplePicker |
shouldContinueAfterSelectingPerson:(ABRecordRef)person { |
[self displayPerson:person]; |
[self dismissModalViewControllerAnimated:YES]; |
return NO; |
} |
- (BOOL)peoplePickerNavigationController: |
(ABPeoplePickerNavigationController *)peoplePicker |
shouldContinueAfterSelectingPerson:(ABRecordRef)person |
property:(ABPropertyID)property |
identifier:(ABMultiValueIdentifier)identifier |
{ |
return NO; |
} |
- (void)displayPerson:(ABRecordRef)person |
{ |
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, |
kABPersonFirstNameProperty); |
self.firstName.text = name; |
NSString* phone = nil; |
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, |
kABPersonPhoneProperty); |
if (ABMultiValueGetCount(phoneNumbers) > 0) { |
phone = (__bridge_transfer NSString*) |
ABMultiValueCopyValueAtIndex(phoneNumbers, 0); |
} else { |
phone = @"[None]"; |
} |
self.phoneNumber.text = phone; |
} |
标签:
原文地址:http://www.cnblogs.com/fantasy3588/p/4889878.html