获取手机通讯录 iOS去除数字以外的所有字符

下面是来客网 jb51.cc 通过网络收集整理的代码片段。

来客网小编现在分享给大家,也给大家做个参考。

//
//  ViewController.m
//  获取手机通讯录
//记得导入头文件#import <AddressBook/AddressBook.h>#import <Contacts/Contacts.h>
//

#import "ViewController.h"
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        CNContactStore *store = [[CNContactStore alloc] init];
        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactFamilyNameKey,CNContactGivenNameKey,CNContactPhoneNumbersKey]];
        NSError *error = nil;
        NSMutableArray *nameArray = [[NSMutableArray alloc] init];
       NSMutableArray *phoneArray = [[NSMutableArray alloc]init];
        //执行获取通讯录请求,若通讯录可获取,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 *strPhone = @"";
            if (contact.phoneNumbers.count>0) {
                strPhone  = [[[contact.phoneNumbers firstObject].value.stringValue componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
            }
            [phoneArray addObject:strPhone];
            NSString *name = @"";
            if ([NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName]) {
                name =  [NSString stringWithFormat:@"%@%@",contact.givenName];
            }
            [nameArray addObject:name];
        }];
        if (flag) {
            NSLog(@"手机号%@",[phoneArray componentsJoinedByString:@","]);
            NSLog(@"名字%@",[nameArray componentsJoinedByString:@","]);
        }
    }else{
        NSMutableArray *nameArray = [[NSMutableArray alloc] init];
        NSMutableArray *phoneArray = [[NSMutableArray alloc]init];
        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 = @"";
                if (array.count > 0) {
                    phoneNumber = [array firstObject];
                }
                
                NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%@%@",familyName,givenName],@"name",phoneNumber,@"phone",[NSNumber numberWithBool:NO],@"isUser",nil];
                //去除数字以外的所有字符
                NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
                                               invertedSet ];
                NSString *strPhone = [[phoneNumber componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
                [phoneArray addObject:strPhone];
                NSString *name = [NSString stringWithFormat:@"%@%@",givenName];
                [nameArray addObject:name];
            }
            NSLog(@"手机号%@","]);
        }
    
    
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

以上是来客网(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。

以上是来客网为你收集整理的获取手机通讯录 iOS去除数字以外的所有字符全部内容,希望文章能够帮你解决获取手机通讯录 iOS去除数字以外的所有字符所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。