Objective-C (iOS): アドレス帳から名前、電話番号、email を取得

AddressViewController

Objective-C: AddressViewController.h

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

@interface AddressViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>

@end

Objective-C: AddressViewController.m

#import "AddressViewController.h"

@interface AddressViewController ()

@end

@implementation AddressViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 150, 50);
    [button setTitle:@"電話帳を開く" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonWasTapped:(UIButton *)button
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSLog(@"%@ %@", firstName, lastName);
    
    // 電話番号
    ABMultiValueRef phoneRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *phone;
    if (ABMultiValueGetCount(phoneRef) > 0) {
        phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phoneRef, 0));
        NSLog(@"%@", phone);
    }
    
    // email
    ABMultiValueRef emailRef = ABRecordCopyValue(person, kABPersonEmailProperty);
    NSString *email;
    if (ABMultiValueGetCount(emailRef) > 0) {
        email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emailRef, 0);
        NSLog(@"%@", email);
    }
    
    // URL
    ABMultiValueRef urlRef = ABRecordCopyValue(person, kABPersonURLProperty);
    NSString *url;
    if (ABMultiValueGetCount(urlRef) > 0) {
        url = (__bridge NSString *)ABMultiValueCopyValueAtIndex(urlRef, 0);
    }
    
    // 住所
    ABMultiValueRef addressRef = ABRecordCopyValue(person, kABPersonAddressProperty);
    NSString *address;
    if (ABMultiValueGetCount(addressRef) > 0) {
        CFDictionaryRef addressData = ABMultiValueCopyValueAtIndex(addressRef, 0);
        CFStringRef street = CFDictionaryGetValue(addressData, kABPersonAddressStreetKey);
        CFStringRef city = CFDictionaryGetValue(addressData, kABPersonAddressCityKey);
        address = [NSString stringWithFormat:@"%@, %@",street,city];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    
    // addressbook の詳細へ移動しない
    return NO;
}

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return YES;
}

@end