iOS学习笔记

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

2016-08-15  本文已影响60人  多啦A梦的百宝袋

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);

}

上一篇下一篇

猜你喜欢

热点阅读