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<contact.phoneNumbers.count; 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开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,424评论 0 3
  • 1、改变 UITextField 占位文字 颜色和去掉底部白框 [_userName setValue:[UICo...
    i_MT阅读 1,064评论 0 2
  • //将NSData转化为NSString NSString* str = [[NSString alloc] in...
    脱脱夫斯基阅读 1,163评论 0 52
  • “黑刺五人组,”一个似曾相识的声音在背后轻声道,“他们要去巨象镇。” 季立从回忆中惊醒,他转头快速地扫了一眼身后的...
    oldj阅读 301评论 0 1