iOS 访问通讯录

来源:互联网 发布:cms内容管理系统 选择 编辑:程序博客网 时间:2024/05/31 18:48

-(NSArray *)phone

{

   

    if ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 9.0) {

        CNContactStore *store = [[CNContactStorealloc] init];

        CNContactFetchRequest *request = [[CNContactFetchRequestalloc] initWithKeysToFetch:@[CNContactFamilyNameKey,CNContactGivenNameKey,CNContactPhoneNumbersKey]];

        NSError *error =nil;

        //执行获取通讯录请求,若通讯录可获取,flagYES,代码块也会执行,若获取失败,flagNO,代码块不执行

        BOOL flag = [storeenumerateContactsWithFetchRequest:requesterror:&error usingBlock:^(CNContact *_Nonnull contact, BOOL *_Nonnull stop) {

            //去除数字以外的所有字符

            NSCharacterSet *setToRemove = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]

                                           invertedSet ];

            NSString *name =@"";

            if ([NSStringstringWithFormat:@"%@%@",contact.familyName,contact.givenName]) {

                name =  [NSStringstringWithFormat:@"%@%@",contact.familyName,contact.givenName];

            }

            

            NSString *strPhone =@"";

            if (contact.phoneNumbers.count>0) {

                strPhone  = [[[contact.phoneNumbersfirstObject].value.stringValuecomponentsSeparatedByCharactersInSet:setToRemove]componentsJoinedByString:@""];

            }

            if ([[namestringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]>0)

            {

                [nameArrayaddObject:name];

                [phoneArrayaddObject:strPhone];

            }

        }];

        if (flag) {

            NSLog(@"手机号%@",phoneArray);

            NSLog(@"名字%@",[nameArraycomponentsJoinedByString:@","]);

        }

    }

    else{

        CFErrorRef *error =nil;

        ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, error);

        __blockBOOL accessGranted = NO;

        

        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);

        

        if (accessGranted) {

            

            CFArrayRef allPeople =ABAddressBookCopyArrayOfAllPeople (addressBook);

            CFIndex nPeople =ABAddressBookGetPersonCount (addressBook);

            

            

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

            {

                ABRecordRef person =CFArrayGetValueAtIndex (allPeople, i);

                NSString *givenName = (__bridgeNSString *)(ABRecordCopyValue (person,kABPersonFirstNameProperty )) == nil ? @"" : (__bridgeNSString *)(ABRecordCopyValue (person,kABPersonFirstNameProperty ));

                NSString *familyName = (__bridgeNSString *)(ABRecordCopyValue (person,kABPersonLastNameProperty )) == nil ? @"" : (__bridgeNSString *)(ABRecordCopyValue (person,kABPersonLastNameProperty ));

                ABMultiValueRef phoneNumbers =ABRecordCopyValue(person, kABPersonPhoneProperty);

                NSArray *array =CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumbers));

                NSString *phoneNumber =@"";

                if (array.count >0) {

                    phoneNumber = [array firstObject];

                }

                //去除数字以外的所有字符

                NSCharacterSet *setToRemove = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]

                                               invertedSet ];

                NSString *strPhone = [[phoneNumbercomponentsSeparatedByCharactersInSet:setToRemove]componentsJoinedByString:@""];

                NSString *name = [NSStringstringWithFormat:@"%@%@",familyName,givenName];

                if ([[namestringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]>0)

                {

                    [nameArrayaddObject:name];

                    [phoneArrayaddObject:strPhone];

                }

            }

            NSLog(@"手机号%@",[phoneArraycomponentsJoinedByString:@","]);

            NSLog(@"名字%@",[nameArraycomponentsJoinedByString:@","]);

        }

        

        

    }

    returnphoneArray;

}

0 0
原创粉丝点击