iOS开发之系统通讯录

                      @iOS调用操作通讯录所用的库文件

                                        AddressBook.framework

                                        AddressBookUI.framework

#import "HMTMainViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface HMTMainViewController ()<ABPeoplePickerNavigationControllerDelegate>

@property (nonatomic,strong) ABPeoplePickerNavigationController *personPickVC;
@property (nonatomic,strong) UIWebView *phoneCallWebView;

@end

@implementation HMTMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"系统通讯录";
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDeletePersonAction)];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 70, 120, 40);
    [button setTitle:@"选择联系人" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}


- (void)didClickSelectAction{
    
    //  选择电话薄的联系人
    self.personPickVC = [[ABPeoplePickerNavigationController alloc] init];
    _personPickVC.peoplePickerDelegate = self;
    _personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
    [self deletePerson];
    [self presentViewController:_personPickVC animated:YES completion:NULL];
    //[self.navigationController pushViewController:[_personPickVC.viewControllers objectAtIndex:0] animated:YES];
    //[self getAllPerson];
    
    
    
}

//  进入选择联系人界面
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

    //  选中联系人后,就退出,并返回想要的属性
    [peoplePicker dismissViewControllerAnimated:YES completion:^{
       
        NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"person = %@",firstName);
        
        ABMultiValueRef phones = ABRecordCopyValue(person,kABPersonPhoneProperty);
        for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
            NSLog(@"telephone = %@",phone);
        }
    }];
    
    return YES;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{

    [peoplePicker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

    return YES;
}

//  展示所有联系人
- (void)getAllPerson{

    CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
    for (id person in ((__bridge NSArray *)allPerson)) {
        
        NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
        NSLog(@"firstName = %@",firstName);
 
        //  因为一个用户可能有多个电话,所以需要循环调取
        ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonPhoneProperty);
        for (int i = 0; i < ABMultiValueGetCount(phones); i++) {
            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
            NSLog(@"telephone = %@",phone);
        }
        
    }
}

//  添加联系人
- (void)addPerson{

    ABAddressBookRef addressBook = self.personPickVC.addressBook;
    ABRecordRef person = ABPersonCreate();
    ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil);
    ABRecordSetValue(person, kABPersonLastNameProperty, @"明涛", nil);
    ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType);
    for (int i = 0; i < 1; i++) {
        ABMultiValueIdentifier mutableIdentifier;
        ABMultiValueAddValueAndLabel(mulRef, @"18690231234", nil, &mutableIdentifier);
    }
    ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, nil);
    ABAddressBookAddRecord(addressBook, person, nil);
    ABAddressBookSave(addressBook, nil);
    
}

//  删除联系人
- (void)deletePerson{
    
    CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook);
    for (id person in (__bridge NSArray *)allPerson) {
        
        NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty);
        if ([firstName isEqualToString:@"胡"]) {
            ABAddressBookRemoveRecord(self.personPickVC.addressBook, (__bridge ABRecordRef)person, nil);
        }
    }
    ABAddressBookSave(self.personPickVC.addressBook, nil);
}

//  拨打电话
- (void)dialPhoneNumber:(NSString *)phoneNumber{

    // 1.UIWebView加载电话
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
    self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    [self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
    
    // 2.私有方法
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];
}


iOS开发之系统通讯录,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。