IOS讀取手機聯(lián)系人,寫入文件中

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(@"--------------結(jié)束------------------%@",blackList);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS開發(fā)系列--網(wǎng)絡開發(fā) 概覽 大部分應用程序都或多或少會牽扯到網(wǎng)絡開發(fā),例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 4,058評論 2 7
  • 1、設(shè)置UILabel行間距 NSMutableAttributedString* attrString = [[...
    FF_911閱讀 1,502評論 0 3
  • 1、改變 UITextField 占位文字 顏色和去掉底部白框 [_userName setValue:[UICo...
    i_MT閱讀 1,197評論 0 2
  • //將NSData轉(zhuǎn)化為NSString NSString* str = [[NSString alloc] in...
    脫脫夫斯基閱讀 1,266評論 0 52
  • “黑刺五人組,”一個似曾相識的聲音在背后輕聲道,“他們要去巨象鎮(zhèn)?!?季立從回憶中驚醒,他轉(zhuǎn)頭快速地掃了一眼身后的...
    oldj閱讀 348評論 0 1

友情鏈接更多精彩內(nèi)容