IOS读取手机联系人,写入文件中

NSMutableString *muStr = [NSMutableString string];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);

NSString *cachesDirectoryPath = [paths objectAtIndex:0];

NSString *blackList = [cachesDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",text.text]];

CNContactStore *store2=[[CNContactStore alloc]init];

CNContactFetchRequest *request=[[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactPhoneticFamilyNameKey,CNContactPhoneNumbersKey]];

[store2 enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

if (contact.phoneNumbers.count>2) {

  for (int i=1; i

      CNLabeledValue *label= contact.phoneNumbers[i];

      NSString  *person = label.label;

     NSLog(@"============%@",person);

     CNPhoneNumber *iphoneNumber = label.value;

     NSString *number = iphoneNumber.stringValue;

     NSLog(@"============%@",number);

     NSString *lineManNumber = [NSString stringWithFormat:@"%@:%@",person,number];

     [muStr appendFormat:@"%@,",number];

    }

 }

}];

//写文件

NSError *error;

[muStr writeToFile:blackList atomically:YES encoding:NSUTF8StringEncoding error:&error];

if (error) {

NSLog(@"错误。。。。。%@",error);

}else{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"正确" message:blackList delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

[alert show];

NSLog(@"正确。。。。。");

NSLog(@"--------------结束------------------%@",blackList);

}

你可能感兴趣的:(IOS读取手机联系人,写入文件中)