日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

读取手机联系人信息

發布時間:2025/6/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 读取手机联系人信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

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

? ? ? ? NSMutableArray *ary = [[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:@"" ];

? ? ? ? ? ? ? ? NSLog(@"%@",strPhone);

? ? ? ? ? ? }

? ? ? ? ? ? //[phoneArray addObject:strPhone];

? ? ? ? ? ? NSString *name = @"";

? ? ? ? ? ? if ([NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName]) {

? ? ? ? ? ? ? ? name =? [NSString stringWithFormat:@"%@ %@",contact.familyName,contact.givenName];

? ? ? ? ? ? }

? ? ? ? ? ? NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

? ? ? ? ? ? [dict setObject:strPhone forKey:@"phoneNum"];

? ? ? ? ? ? [dict setObject:name forKey:@"name"];

? ? ? ? ? ? //[nameArray addObject:name];

? ? ? ? ? ? [ary addObject:dict];

? ? ? ? }];

? ? ? ? if (flag) {

//? ? ? ? ? ? NSLog(@"手機號%@",[phoneArray componentsJoinedByString:@","]);

//? ? ? ? ? ? NSLog(@"名字%@",[nameArray componentsJoinedByString:@","]);

? ? ? ? ? ? NSLog(@"%@",ary);

? ? ? ? }

? ? }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];

? ? ? ? ? ? ? ? NSLog(@"%@",dic);

? ? ? ? ? ? ? ? //去除數字以外的所有字符

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

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? invertedSet ];

? ? ? ? ? ? ? ? NSString *strPhone = [[phoneNumber componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];

? ? ? ? ? ? ? ? [phoneArray addObject:strPhone];

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

? ? ? ? ? ? ? ? [nameArray addObject:name];

? ? ? ? ? ? }

? ? ? ? ? ? NSLog(@"手機號%@",[phoneArray componentsJoinedByString:@","]);

? ? ? ? ? ? NSLog(@"名字%@",[nameArray componentsJoinedByString:@","]);

? ? ? ? }

?? ? ? ?

?? ? ? ?

? ? }


轉載于:https://my.oschina.net/BurningOC/blog/530016

總結

以上是生活随笔為你收集整理的读取手机联系人信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。