I am getting on OK with my Objective C learning but have hit a bump in my app.
Below is the code I have so far, just a simple one button app with the plan to be that clicking the button will open the contacts, let you select a contact and then choose a specific number from those available which is saved as the text of a field eventually. I have got this far but I get as far as clicking the number within the contacts list and nothing happens.
.h file:
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
@interface ViewController : UIViewController <UINavigationControllerDelegate, ABPeoplePickerNavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)myButton:(UIButton *)sender;
@end
.m file:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)myButton:(UIButton *)sender {
ABPeoplePickerNavigationController *peoplePicker =
[[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)pickershouldContinueAfterSelectingPerson :
(ABRecordRef)person {
NSString* name =
(__bridge_transfer NSString *)ABRecordCopyCompositeName(person);
ABMutableMultiValueRef phones =
ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray *numbers =
(__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(phones);
ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *addresses =
(__bridge_transfer NSString *)ABMultiValueCopyArrayOfAllValues(emails);
NSString *note =
(__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonNoteProperty);
NSLog( @"name = %@, numbers = %@, email = %@, note = %@",
name, numbers, addresses, note );
// WANT TO MAKE FOR EXAMPLE ( myTextField.text = CHOSEN NUMBER OF CONTACT
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
First, declare your conformance to
ABPersonViewControllerDelegate
in your .h file.Second, implement the following
ABPersonViewControllerDelegate
callback method in your class to enumerate the selected contact's phone numbers: