From db36c62adb30cdb00c306b5250670079ca95f2dd Mon Sep 17 00:00:00 2001 From: coderZsq Date: Sun, 29 Sep 2019 17:40:14 +0800 Subject: [PATCH] add --- .../Connections/SQConnectionModel.h | 1 + .../Connections/SQConnectionsViewController.m | 17 ++-- .../SQManagement/Tools/SQAuthorizationTool.h | 33 ++++++- .../SQManagement/Tools/SQAuthorizationTool.m | 89 +++++++++++++++++-- 4 files changed, 128 insertions(+), 12 deletions(-) diff --git a/SQManagement/SQManagement/Connections/SQConnectionModel.h b/SQManagement/SQManagement/Connections/SQConnectionModel.h index 3f002465..d2dab00b 100644 --- a/SQManagement/SQManagement/Connections/SQConnectionModel.h +++ b/SQManagement/SQManagement/Connections/SQConnectionModel.h @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SQConnectionModel : NSObject +@property (nonatomic, strong) NSData *profile; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *role; @property (nonatomic, copy) NSString *occupation; diff --git a/SQManagement/SQManagement/Connections/SQConnectionsViewController.m b/SQManagement/SQManagement/Connections/SQConnectionsViewController.m index 5863bedb..6932773b 100644 --- a/SQManagement/SQManagement/Connections/SQConnectionsViewController.m +++ b/SQManagement/SQManagement/Connections/SQConnectionsViewController.m @@ -23,8 +23,14 @@ - (void)viewDidLoad { [super viewDidLoad]; self.title = @"人脉"; self.dataSource = [NSMutableArray array]; - [SQAuthorizationTool fetchContacts:^(NSString *name, NSArray *phoneNumbers) { - [self.dataSource addObject:name]; + [SQAuthorizationTool fetchContacts:^(NSArray * _Nonnull contacts) { + for (SQContact *contact in contacts) { + SQConnectionModel *model = [SQConnectionModel new]; + model.profile = contact.imageData; + model.name = [NSString stringWithFormat:@"%@%@", contact.familyName, contact.givenName]; + model.occupation = contact.jobTitle; + [self.dataSource addObject:model]; + } [self.tableView reloadData]; }]; } @@ -47,7 +53,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"]; } - cell.textLabel.text = self.dataSource[indexPath.row]; + SQConnectionModel *model = self.dataSource[indexPath.row]; + cell.textLabel.text = model.name; return cell; } @@ -64,9 +71,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSIntege - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; SQConnectionEventsViewController *connectionEventsVc = [[UIStoryboard storyboardWithName:NSStringFromClass(SQConnectionEventsViewController.class) bundle:nil] instantiateInitialViewController]; - SQConnectionModel *connection = [SQConnectionModel new]; - connection.name = self.dataSource[indexPath.row]; - connectionEventsVc.connection = connection; + connectionEventsVc.connection = self.dataSource[indexPath.row]; [self.navigationController pushViewController:connectionEventsVc animated:YES]; } diff --git a/SQManagement/SQManagement/Tools/SQAuthorizationTool.h b/SQManagement/SQManagement/Tools/SQAuthorizationTool.h index be0ce0f8..8095958b 100644 --- a/SQManagement/SQManagement/Tools/SQAuthorizationTool.h +++ b/SQManagement/SQManagement/Tools/SQAuthorizationTool.h @@ -10,9 +10,40 @@ NS_ASSUME_NONNULL_BEGIN +@interface SQContact : NSObject +@property (nonatomic, copy) NSString *identifier; +@property (nonatomic, copy) NSString *namePrefix; +@property (nonatomic, copy) NSString *givenName; +@property (nonatomic, copy) NSString *middleName; +@property (nonatomic, copy) NSString *familyName; +@property (nonatomic, copy) NSString *previousFamilyName; +@property (nonatomic, copy) NSString *nameSuffix; +@property (nonatomic, copy) NSString *nickname; +@property (nonatomic, copy) NSString *organizationName; +@property (nonatomic, copy) NSString *departmentName; +@property (nonatomic, copy) NSString *jobTitle; +@property (nonatomic, copy) NSString *phoneticGivenName; +@property (nonatomic, copy) NSString *phoneticMiddleName; +@property (nonatomic, copy) NSString *phoneticFamilyName; +@property (nonatomic, copy) NSString *phoneticOrganizationName; +@property (nonatomic, strong) NSDateComponents *birthday; +@property (nonatomic, strong) NSDateComponents *nonGregorianBirthday; +@property (nonatomic, strong) NSData *imageData; +@property (nonatomic, strong) NSData *thumbnailImageData; +@property (nonatomic, assign) BOOL imageDataAvailable; +@property (nonatomic, strong) NSArray *phoneNumbers; +@property (nonatomic, strong) NSArray *emailAddresses; +@property (nonatomic, strong) NSArray *postalAddresses; +@property (nonatomic, strong) NSArray *dates; +@property (nonatomic, strong) NSArray *urlAddresses; +@property (nonatomic, strong) NSArray *contactRelations; +@property (nonatomic, strong) NSArray *socialProfiles; +@property (nonatomic, strong) NSArray *instantMessageAddresses; +@end + @interface SQAuthorizationTool : NSObject -+ (void)fetchContacts: (void(^)(NSString *, NSArray *))callback; ++ (void)fetchContacts: (void(^)(NSArray *contacts))callback; @end diff --git a/SQManagement/SQManagement/Tools/SQAuthorizationTool.m b/SQManagement/SQManagement/Tools/SQAuthorizationTool.m index 28c8e61f..37f73210 100644 --- a/SQManagement/SQManagement/Tools/SQAuthorizationTool.m +++ b/SQManagement/SQManagement/Tools/SQAuthorizationTool.m @@ -9,9 +9,12 @@ #import "SQAuthorizationTool.h" #import +@implementation SQContact +@end + @implementation SQAuthorizationTool -+ (void)fetchContacts:(void (^)(NSString *, NSArray *))callback { ++ (void)fetchContacts:(void (^)(NSArray * _Nonnull))callback{ dispatch_async(dispatch_get_main_queue(), ^{ CNContactStore *store = [[CNContactStore alloc] init]; CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType: CNEntityTypeContacts]; @@ -24,18 +27,94 @@ + (void)fetchContacts:(void (^)(NSString *, NSArray *))callback { } }]; } - CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactFamilyNameKey, CNContactGivenNameKey,CNContactPhoneNumbersKey]]; + NSArray *keys = @[ + CNContactIdentifierKey, + CNContactNamePrefixKey, + CNContactGivenNameKey, + CNContactMiddleNameKey, + CNContactFamilyNameKey, + CNContactPreviousFamilyNameKey, + CNContactNameSuffixKey, + CNContactNicknameKey, + CNContactOrganizationNameKey, + CNContactDepartmentNameKey, + CNContactJobTitleKey, + CNContactPhoneticGivenNameKey, + CNContactPhoneticMiddleNameKey, + CNContactPhoneticFamilyNameKey, + CNContactPhoneticOrganizationNameKey, + CNContactBirthdayKey, + CNContactNonGregorianBirthdayKey, + CNContactImageDataKey, + CNContactThumbnailImageDataKey, + CNContactImageDataAvailableKey, + CNContactTypeKey, + CNContactPhoneNumbersKey, + CNContactEmailAddressesKey, + CNContactPostalAddressesKey, + CNContactDatesKey, + CNContactUrlAddressesKey, + CNContactRelationsKey, + CNContactSocialProfilesKey, + CNContactInstantMessageAddressesKey + ]; + CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys]; + NSMutableArray *array = [NSMutableArray array]; [store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { - NSString *name = [contact.familyName stringByAppendingString:contact.givenName]; + SQContact *obj = [SQContact new]; + obj.identifier = contact.identifier; + obj.namePrefix = contact.namePrefix; + obj.givenName = contact.givenName; + obj.middleName = contact.middleName; + obj.familyName = contact.familyName; + obj.previousFamilyName = contact.previousFamilyName; + obj.nameSuffix = contact.nameSuffix; + obj.nickname = contact.nickname; + obj.organizationName = contact.organizationName; + obj.departmentName = contact.departmentName; + obj.jobTitle = contact.jobTitle; + obj.phoneticGivenName = contact.phoneticGivenName; + obj.phoneticMiddleName = contact.phoneticMiddleName; + obj.phoneticFamilyName = contact.phoneticFamilyName; + obj.phoneticOrganizationName = contact.phoneticOrganizationName; + obj.birthday = contact.birthday; + obj.nonGregorianBirthday = contact.nonGregorianBirthday; + obj.imageData = contact.imageData; + obj.thumbnailImageData = contact.thumbnailImageData; + obj.imageDataAvailable = contact.imageDataAvailable; NSMutableArray *phoneNumbers = @[].mutableCopy; for (CNLabeledValue *phoneNumber in contact.phoneNumbers) { CNPhoneNumber *phoneNumberValue = phoneNumber.value; [phoneNumbers addObject:phoneNumberValue.stringValue]; } - if (callback) { - callback(name, phoneNumbers); + obj.phoneNumbers = phoneNumbers; + NSMutableArray *emailAddresses = @[].mutableCopy; + for (CNLabeledValue *emailAddress in contact.emailAddresses) { + [emailAddresses addObject:[CNLabeledValue localizedStringForLabel:emailAddress.label]]; } + obj.emailAddresses = emailAddresses; + NSMutableArray *postalAddresses = @[].mutableCopy; + for (CNLabeledValue *postalAddress in contact.postalAddresses) { + NSString *label = [CNLabeledValue localizedStringForLabel:postalAddress.label]; + id detail = postalAddress.value; + id country = [detail valueForKey:CNPostalAddressCountryKey]; + id state = [detail valueForKey:CNPostalAddressStateKey]; + id city = [detail valueForKey:CNPostalAddressCityKey]; + id street = [detail valueForKey:CNPostalAddressStreetKey]; + id code = [detail valueForKey:CNPostalAddressPostalCodeKey]; + [postalAddresses addObject:[NSString stringWithFormat:@"%@: 国家: %@, 省: %@, 城市: %@, 街道: %@, 邮编: %@", label, country, state, city, street, code]]; + } + obj.postalAddresses = postalAddresses; + obj.dates = contact.dates; + obj.urlAddresses = contact.urlAddresses; + obj.contactRelations = contact.contactRelations; + obj.socialProfiles = contact.socialProfiles; + obj.instantMessageAddresses = contact.instantMessageAddresses; + [array addObject:obj]; }]; + if (callback) { + callback(array); + } }); }