码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发之系统通讯录

时间:2014-07-05 11:02:42      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:通讯录   拨打电话   

                      @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开发之系统通讯录,布布扣,bubuko.com

iOS开发之系统通讯录

标签:通讯录   拨打电话   

原文地址:http://blog.csdn.net/hmt20130412/article/details/36692021

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!