您的位置:首页 > 移动开发 > IOS开发

iOS 访问通讯录

2016-03-01 14:47 309 查看
-(NSArray *)phone
{
   
    if ([[[UIDevice
currentDevice] systemVersion]
floatValue] >= 9.0) {
        CNContactStore *store = [[CNContactStore
alloc] init];
        CNContactFetchRequest *request = [[CNContactFetchRequest
alloc] initWithKeysToFetch:@[CNContactFamilyNameKey,CNContactGivenNameKey,CNContactPhoneNumbersKey]];
        NSError *error =
nil;
        //执行获取通讯录请求,若通讯录可获取,flag为YES,代码块也会执行,若获取失败,flag为NO,代码块不执行
        BOOL flag = [store
enumerateContactsWithFetchRequest:request
error:&error usingBlock:^(CNContact *
_Nonnull contact, BOOL *
_Nonnull stop) {
            //去除数字以外的所有字符
            NSCharacterSet *setToRemove = [[
NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
                                          
invertedSet ];
            NSString *name =
@"";
            if ([NSString
stringWithFormat:@"%@%@",contact.familyName,contact.givenName]) {
                name =  [NSString
stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
            }
            
            NSString *strPhone =
@"";
            if (contact.phoneNumbers.count>0)
{
                strPhone  = [[[contact.phoneNumbers
firstObject].value.stringValue
componentsSeparatedByCharactersInSet:setToRemove]
componentsJoinedByString:@""];
            }
            if ([[name
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]] length]>0)
            {
                [nameArray
addObject:name];
                [phoneArray
addObject:strPhone];
            }
        }];
        if (flag) {
            NSLog(@"手机号%@",phoneArray);
            NSLog(@"名字%@",[nameArray
componentsJoinedByString:@","]);
        }
    }
    else{
        CFErrorRef *error =
nil;
        ABAddressBookRef addressBook =
ABAddressBookCreateWithOptions(NULL, error);
        __block
BOOL 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 = (__bridge
NSString *)(ABRecordCopyValue (person,
kABPersonFirstNameProperty )) ==
nil ? @"" : (__bridge
NSString *)(ABRecordCopyValue (person,
kABPersonFirstNameProperty ));
                NSString *familyName = (__bridge
NSString *)(ABRecordCopyValue (person,
kABPersonLastNameProperty )) ==
nil ? @"" : (__bridge
NSString *)(ABRecordCopyValue (person,
kABPersonLastNameProperty ));
                ABMultiValueRef phoneNumbers =
ABRecordCopyValue(person,
kABPersonPhoneProperty);
                NSArray *array =
CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumbers));
                NSString *phoneNumber =
@"";
b5fc
                if (array.count >
0) {
                    phoneNumber = [array firstObject];
                }
                //去除数字以外的所有字符
                NSCharacterSet *setToRemove = [[
NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
                                              
invertedSet ];
                NSString *strPhone = [[phoneNumber
componentsSeparatedByCharactersInSet:setToRemove]
componentsJoinedByString:@""];
                NSString *name = [NSString
stringWithFormat:@"%@%@",familyName,givenName];
                if ([[name
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]] length]>0)
                {
                    [nameArray
addObject:name];
                    [phoneArray
addObject:strPhone];
                }
            }
            NSLog(@"手机号%@",[phoneArray
componentsJoinedByString:@","]);
            NSLog(@"名字%@",[nameArray
componentsJoinedByString:@","]);
        }
        
        
    }
    return
phoneArray;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C iOS开发 iOS