美文网首页iOS学习笔记
IOS读取手机联系人,写入文件中

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

作者: 多啦A梦的百宝袋 | 来源:发表于2016-08-15 12:45 被阅读60次

    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读取手机联系人,写入文件中

        本文链接:https://www.haomeiwen.com/subject/obdusttx.html