SPSBMakePhoneCallManager.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // SPSBMakePhoneCallManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/22.
  6. //
  7. #import "SPSBMakePhoneCallManager.h"
  8. #import "SPSBActionSheetViewController.h"
  9. #import "SPSBUIGeneralHeader.h"
  10. #import "SPSBKeyProfile.h"
  11. #import <ContactsUI/ContactsUI.h>
  12. #import <Contacts/Contacts.h>
  13. @interface SPSBMakePhoneCallManager ()<CNContactPickerDelegate, CNContactViewControllerDelegate> {
  14. __weak UIViewController *_vc;
  15. }
  16. @end
  17. @implementation SPSBMakePhoneCallManager
  18. - (instancetype)initWithController:(UIViewController *)vc {
  19. self = [super init];
  20. if (!self) return nil;
  21. _vc = vc;
  22. return self;
  23. }
  24. - (void)showHandleContactMenu {
  25. SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new;
  26. @weakify(self)
  27. [vc addTopView:[self creatActionSheetTipsView] height:0];
  28. [vc addActionWithTitle:@"直接呼叫" action:^{
  29. @strongify(self)
  30. [self makePhoneCall];
  31. }];
  32. [vc addActionWithTitle:@"添加到手机通讯录" action:^{
  33. @strongify(self)
  34. [self addToPhoneAddressBook];
  35. }];
  36. [_vc presentViewController:vc animated:false completion:nil];
  37. }
  38. - (void)makePhoneCall {
  39. NSMutableString *phone = [jxh_userDefaults() objectForKey:SPSBCustomerServicePhone];
  40. NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", phone];
  41. jxh_openUrl([NSURL URLWithString:callPhone], ^(BOOL success) {
  42. });
  43. }
  44. - (void)addToPhoneAddressBook {
  45. SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new;
  46. @weakify(self)
  47. [vc addActionWithTitle:@"创建新联系人" action:^{
  48. @strongify(self)
  49. [self createNewContact];
  50. }];
  51. [vc addActionWithTitle:@"添加到现有联系人" action:^{
  52. @strongify(self)
  53. [self updateContact];
  54. }];
  55. [_vc presentViewController:vc animated:false completion:nil];
  56. }
  57. - (void)createNewContact {
  58. CNContactViewController *contactVC = [CNContactViewController viewControllerForNewContact:[self initializeContact]];
  59. contactVC.delegate = self;
  60. UINavigationController *nav_contactVC = [[UINavigationController alloc] initWithRootViewController:contactVC];
  61. nav_contactVC.modalPresentationStyle = UIModalPresentationFullScreen;
  62. [_vc presentViewController:nav_contactVC animated:true completion:nil];
  63. }
  64. - (CNMutableContact *)initializeContact {
  65. // 创建联系人对象
  66. CNMutableContact *contact = [[CNMutableContact alloc] init];
  67. // 设置电话号码
  68. CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:[jxh_userDefaults() objectForKey:SPSBCustomerServicePhone]];
  69. CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:SPSBAppName value:mobileNumber];
  70. contact.phoneNumbers = @[mobilePhone];
  71. return contact;
  72. }
  73. - (void)updateContact {
  74. // 初始化CNContactPickerViewController
  75. CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
  76. contactPickerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  77. // 设置代理
  78. contactPickerViewController.delegate = self;
  79. // 显示联系人窗口视图
  80. [_vc presentViewController:contactPickerViewController animated:true completion:nil];
  81. }
  82. - (void)updateContact:(CNMutableContact *)contact {
  83. // 创建联系人请求
  84. CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
  85. [saveRequest updateContact:contact];
  86. // 重新写入
  87. CNContactStore *store = [[CNContactStore alloc] init];
  88. [store executeSaveRequest:saveRequest error:nil];
  89. }
  90. - (void)addContact:(CNMutableContact *)contact {
  91. // 创建联系人请求
  92. CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
  93. [saveRequest addContact:contact toContainerWithIdentifier:nil];
  94. // 写入联系人
  95. CNContactStore *store = [[CNContactStore alloc] init];
  96. [store executeSaveRequest:saveRequest error:nil];
  97. }
  98. - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
  99. [_vc dismissViewControllerAnimated:true completion:nil];
  100. NSMutableArray *numbers = [contact.phoneNumbers mutableCopy];
  101. CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:[jxh_userDefaults() objectForKey:SPSBCustomerServicePhone]];
  102. CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:SPSBAppName value:mobileNumber];
  103. [numbers addObject:mobilePhone];
  104. CNMutableContact *newContact = [contact mutableCopy];
  105. newContact.phoneNumbers = numbers;
  106. CNContactViewController *contactVC = [CNContactViewController viewControllerForNewContact:newContact];
  107. contactVC.delegate = self;
  108. UINavigationController *nav_contactVC = [[UINavigationController alloc] initWithRootViewController:contactVC];
  109. nav_contactVC.modalPresentationStyle = UIModalPresentationFullScreen;
  110. [_vc presentViewController:nav_contactVC animated:true completion:nil];
  111. }
  112. - (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact {
  113. [viewController.navigationController dismissViewControllerAnimated:true completion:nil];
  114. }
  115. - (UIView *)creatActionSheetTipsView {
  116. UIView *tipsView = UIView.new;
  117. tipsView.backgroundColor = spsb_F5F5F5_color();
  118. UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(14) textColor:spsb_999999_color()limitWidth:jxh_screenWidth()];
  119. [titleLabel setAttributedStringWithText:@"服务时间为周一至周五9:00~12:00,13:30~17:30\n其他时间拨打可能无人接听" textAlignment:NSTextAlignmentCenter lineSpacing:4 lineHeight:0 paragraphSpacing:0 lineBreakMode:NSLineBreakByWordWrapping lines:2];
  120. [tipsView addSubview:titleLabel];
  121. [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  122. make.top.equalTo(15);
  123. make.bottom.equalTo(-15);
  124. make.centerX.equalTo(0);
  125. }];
  126. return tipsView;
  127. }
  128. @end