zoukankan      html  css  js  c++  java
  • 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"]];
    }
    


    @有的时候,我们按上诉方法訪问通讯录无效,这是由于,iOS6之后加强了对通讯录的訪问,要求我们显示的申请去訪问通讯录,加上例如以下几句代码就OK了:

        CFErrorRef *error = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        // Request authorization to Address Book
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                    if (granted) {
                        // First time access has been granted, add the contact
                        //[self _addContactToAddressBook];
                    } else {
                        // User denied access
                        // Display an alert telling user the contact could not be added
                    }
                });
            }
            else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
                // The user has previously given access, add the contact
                //[self _addContactToAddressBook];
            }
            else {
                // The user has previously denied access
                // Send an alert telling user to change privacy setting in settings app
        }
    



查看全文
  • 相关阅读:
    创建及修改PDF文件
    Menu控件几个使用方法
    IE不能运行有JS代码的网页处理方法
    SSO实现
    css+div文字底部对齐
    数据控件嵌套的几种方法
    打开及关闭javascript代码
    Excel导出及数据格式化处理
    Menu使用
    在RHEL_5环境,使用centos源
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10837830.html
  • Copyright © 2011-2022 走看看