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;
}
{
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;
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS开发之路--微博“更多”页面
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- Objective-C的内省(Introspection)用法小结
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事