代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
获取手机通讯录 iOS去除数字以外的所有字符
作者:
韩万杰
/ 发布于
2015/11/12
/
1633
// // 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.familyName,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:@"%@%@",familyName,givenName]; [nameArray addObject:name]; } NSLog(@"手机号%@",[phoneArray componentsJoinedByString:@","]); NSLog(@"名字%@",[nameArray componentsJoinedByString:@","]); } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
试试其它关键字
手机通讯录
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
韩万杰
贡献的其它代码
(
13
)
.
获取当前手机系统语言
.
获取手机通讯录 iOS去除数字以外的所有字符
.
iOS 搜索框
.
GLUT示例
.
UIWebView 加载本地网页
.
取消导航条对视图的影响
.
利用终端显示 隐藏文件
.
更改导航条背景颜色
.
iOS 字体斜体
.
获取app版本号 plist文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3