RAContactHelper.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // RAContact.m
  3. // APEX CRM
  4. //
  5. // Created by Jack on 2018/12/15.
  6. // Copyright © 2018 USAI. All rights reserved.
  7. //
  8. #import "RAContactHelper.h"
  9. #import <Contacts/Contacts.h>
  10. #import <ContactsUI/ContactsUI.h>
  11. static RAContactHelper *sharedInstance;
  12. @interface RAContactHelper () <CNContactPickerDelegate>
  13. @property (nonatomic,copy) void(^pickerCompleteBlk)(BOOL canceled, NSArray<CNContact *> *contacts);
  14. @end
  15. @implementation RAContactHelper
  16. + (instancetype)defaultHelper {
  17. static dispatch_once_t token;
  18. dispatch_once(&token, ^{
  19. sharedInstance = [RAContactHelper new];
  20. });
  21. return sharedInstance;
  22. }
  23. - (void)showContactPickerByViewController:(UIViewController *)viewController withComplete:(void(^)(BOOL canceled, NSArray<CNContact *> *contacts))complete {
  24. if (viewController) {
  25. CNContactPickerViewController *pickerVC = [[CNContactPickerViewController alloc] init];
  26. pickerVC.delegate = self;
  27. [viewController presentViewController:pickerVC animated:YES completion:^{
  28. }];
  29. self.pickerCompleteBlk = [complete copy];
  30. }
  31. }
  32. #pragma mark - Contact
  33. - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
  34. if (self.pickerCompleteBlk) {
  35. self.pickerCompleteBlk(YES, nil);
  36. }
  37. self.pickerCompleteBlk = nil;
  38. }
  39. // 四个select代理方法只用实现一个就可以
  40. // select contact 优先级比 select contact property高
  41. // 多选 优先级 比单选 高
  42. - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
  43. if (self.pickerCompleteBlk) {
  44. self.pickerCompleteBlk(NO, contacts);
  45. }
  46. self.pickerCompleteBlk = nil;
  47. }
  48. //- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty *> *)contactProperties {
  49. //
  50. //}
  51. //
  52. //- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
  53. //
  54. //}
  55. //
  56. //- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
  57. //
  58. //}
  59. @end