ios 简书 获取通讯录信息_iOS-授权获取通讯录
- (void)getContact{
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if(authorizationStatus ==CNAuthorizationStatusAuthorized) {
// 獲取指定的字段,并不是要獲取所有字段,需要指定具體的字段
NSArray*keysToFetch =@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey];
CNContactFetchRequest*fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
CNContactStore*contactStore = [[CNContactStore alloc]init];
//創建一個保存通訊錄的數組
NSMutableArray *contactArr = [NSMutableArray array];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact*_Nonnull contact,BOOL*_Nonnull stop) {
NSLog(@"-------------------------------------------------------");
NSString*givenName = contact.givenName;
NSString*familyName = contact.familyName;
NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
NSArray*phoneNumbers = contact.phoneNumbers;
for(CNLabeledValue*labelValue in phoneNumbers) {
NSString*label = labelValue.label;
CNPhoneNumber*phoneNumber = labelValue.value;
NSDictionary*contact =@{@"phone":phoneNumber.stringValue,@"user":FORMAT(@"%@%@",familyName,givenName)};
[contactArr addObject:contact];
NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
}
//*stop = YES;// 停止循環,相當于break;
}];
_contactArr= contactArr;
NSError*error;
NSData*jsonData = [NSJSONSerialization dataWithJSONObject:contactArr options:NSJSONWritingPrettyPrinted error:&error];//此處data參數是我上面提到的key為"data"的數組
NSString*jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
_jsonString= jsonString;
NSLog(@"jsonString====%@",jsonString);
[self postContactTo]; //6.上傳通訊錄
}else{
NSLog(@"====通訊錄沒有授權====");
}
}
總結
以上是生活随笔為你收集整理的ios 简书 获取通讯录信息_iOS-授权获取通讯录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis3 没有生成example
- 下一篇: python的基本语法和语言_Pytho