Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderZsq committed Sep 29, 2019
1 parent 15e788d commit db36c62
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 12 deletions.
1 change: 1 addition & 0 deletions SQManagement/SQManagement/Connections/SQConnectionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}];
}
Expand All @@ -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;
}

Expand All @@ -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];
}

Expand Down
33 changes: 32 additions & 1 deletion SQManagement/SQManagement/Tools/SQAuthorizationTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 84 additions & 5 deletions SQManagement/SQManagement/Tools/SQAuthorizationTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
#import "SQAuthorizationTool.h"
#import <Contacts/Contacts.h>

@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];
Expand All @@ -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);
}
});
}

Expand Down

0 comments on commit db36c62

Please sign in to comment.