码迷,mamicode.com
首页 > 其他好文 > 详细

获取联系--(不需要弹出控制器)

时间:2016-09-05 23:28:51      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

#pragma mark 获取通讯录

- (NSMutableArray *)accessAddressBookAddress{

    NSMutableArray * addressBookTemp=[[NSMutableArray alloc]initWithCapacity:0];

    //新建一个通讯录类

    ABAddressBookRef addressBooks = nil;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0){

        addressBooks =ABAddressBookCreateWithOptions(NULL, NULL);

        //获取通讯录权限

        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){

            dispatch_semaphore_signal(sema);

        });

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    }else{

        addressBooks = ABAddressBookCreate();

    }

    //获取通讯录中的所有人

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

    //通讯录中人数

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

    //循环,获取每个人的个人信息

    for (NSInteger i = 0; i < nPeople; i++){

        //新建一个addressBook model

        Model *addressBook = [[Model alloc] init];

        //获取个人

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //获取个人名字

        CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

        CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

        CFStringRef abFullName = ABRecordCopyCompositeName(person);

        NSString *nameString = (__bridge NSString *)abName;

        NSString *lastNameString = (__bridge NSString *)abLastName;

        if ((__bridge id)abFullName != nil) {

            nameString = (__bridge NSString *)abFullName;

        } else if ((__bridge id)abLastName != nil){

            nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];

        }

        addressBook.name = nameString;

        addressBook.recordID = (int)ABRecordGetRecordID(person);

        ABPropertyID multiProperties[] = {kABPersonPhoneProperty,kABPersonEmailProperty};

        NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);

        for (NSInteger j = 0; j < multiPropertiesTotal; j++) {

            ABPropertyID property = multiProperties[j];

            ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);

            NSInteger valuesCount = 0;

            if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);

            if (valuesCount == 0) {

                CFRelease(valuesRef);

                continue;

            }

            //获取电话号码和email

            for (NSInteger k = 0; k < valuesCount; k++) {

                CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);

                switch (j) {

                    case 0: {// Phone number

                        addressBook.tel = (__bridge NSString*)value;

                        NSMutableString * tel=[NSMutableString stringWithString:addressBook.tel];

                        [tel replaceOccurrencesOfString:@"-" withString:@"" options:NSCaseInsensitiveSearch  range: NSMakeRange(0, addressBook.tel.length)];

                        addressBook.tel=[tel copy];

                        break;

                    }

                    case 1: {// Email

                        addressBook.email = (__bridge NSString*)value;

                        break;

                    }

                        

                }

                CFRelease(value);

            }

            CFRelease(valuesRef);

        }

        //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

        [addressBookTemp addObject:addressBook];

        

        if (abName) CFRelease(abName);

        if (abLastName) CFRelease(abLastName);

        if (abFullName) CFRelease(abFullName);

        

    }

    return addressBookTemp;

}

获取联系--(不需要弹出控制器)

标签:

原文地址:http://www.cnblogs.com/daxueshan/p/5843985.html

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