iOS-读取系统通讯录

 

iOS-读取系统通讯录

 

 

备注:

1.在ios6.0以上通迅录第一次读取时,需要用户授权(会自动弹出授权提示框),授权同意则可以读取通讯录,如果不同意则无法读取通讯录。

2.一旦授权后,以后将一直按照用户授权(同意或不同意)进行操作,不会再次弹出授权提示框,每个应用都只有1次机会可以弹出通讯录授权提示框,请珍惜!!!

3.读取通讯录的方法建议放在viewDidAppear中执行,如果放在 viewDidLoad方法中,则第1次弹出的授权提示框会看不见,被应用挡住在后面了,如果遇到看不见授权提示框,你可以按下Home键,就可以看见授权提示框了,说明被应用遮住了,所以建议放在viewDidAppear中。

4.用户首次授权后,之后可以在手机的设置-隐私-通讯录中修改应用的授权设置参数。

 

 

导入AddressBook.framework先

引用  #import <AddressBook/AddressBook.h>

 

 

方法doReadAddressBook:(应该支持iOS6或6以上)

 

-(void)doReadAddressBook{

    CFErrorRef *error = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    __block BOOL accessGranted = NO;
    if (&ABAddressBookRequestAccessWithCompletion != NULL) {

        // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }else {
        // we're on iOS 5 or older
        accessGranted = YES;
    }
    
    if (accessGranted) {
        NSLog(@"已经授权同意:accessGranted=%d",accessGranted);
        NSArray* tmpPeoples = (__bridge NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSMutableArray* contacts = [[NSMutableArray alloc]init];
        for(id tmpPerson in tmpPeoples)
            
        {
            BOAddressBook* addressBook = [[BOAddressBook alloc]init];
            
            //获取的联系人单一属性:First name
            NSString* tmpFirstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonFirstNameProperty);
//            NSLog(@"First name:%@", tmpFirstName);
            
            //获取的联系人单一属性:Last name
            NSString* tmpLastName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonLastNameProperty);
//            NSLog(@"Last name:%@", tmpLastName);

            
            //获取的联系人单一属性:Generic phone number
            ABMultiValueRef tmpPhones = ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonPhoneProperty);
            NSMutableArray* phones = [[NSMutableArray alloc]init];
            for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
            {
                BOAddressBookPhone* phone = [[BOAddressBookPhone alloc]init];
                NSString* tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
//                NSLog(@"tmpPhoneIndex%ld:%@", j, tmpPhoneIndex);
                phone.phoneNumber=tmpPhoneIndex;
                [phones addObject:phone];
            }
            
            addressBook.firstName=tmpFirstName;
            addressBook.lastName=tmpLastName;
            addressBook.addressBookPhones=phones;
            
            [contacts addObject:addressBook];
            
            
        }
        [self doReadAddressBookSuccessed:contacts];
        
    }else{
        NSLog(@"已经授权不同意:accessGranted=%d",accessGranted);
        [self doReadAddressBookFailed];
    }
    
}

-(void)doReadAddressBookSuccessed : (NSMutableArray*) contacts{
    //读取通讯录成功,则在此处继续开始下一步功能
    NSLog(@"doReadAddressBookSuccessed");
}

-(void)doReadAddressBookFailed{
    //读取失败:因为用户设备授权不同意
    NSLog(@"doReadAddressBookFailed");
}

 

BOAddressBook.h和BOAddressBookPhone.h

#import <Foundation/Foundation.h>

@interface BOAddressBook : NSObject

@property (nonatomic,strong) NSString* firstName;//first name
@property (nonatomic,strong) NSString* lastName;//last name
@property (nonatomic,strong) NSMutableArray* addressBookPhones;//电话号码:每个人可以有多个号码,所有定义为数组
@end



#import <Foundation/Foundation.h>

@interface BOAddressBookPhone : NSObject

@property (nonatomic,strong) NSString* phoneNumber;//电话号码
@end

 

 

 

 

 

 

 

 

以上doReadAddressBook方法必须在viewDidAppear方法中调用,如下:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self doReadAddressBook];
}

 

 

 

 附件demo010.zip是demo工程项目压缩包。

 

 

 

你可能感兴趣的:(iOS-读取系统通讯录)