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)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容