下面是内存溢出 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去除数字以外的所有字符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)