123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // SPSBMakePhoneCallManager.m
- // 我的社保
- //
- // Created by shanp on 2021/5/22.
- //
- #import "SPSBMakePhoneCallManager.h"
- #import "SPSBActionSheetViewController.h"
- #import "SPSBUIGeneralHeader.h"
- #import "SPSBKeyProfile.h"
- #import <ContactsUI/ContactsUI.h>
- #import <Contacts/Contacts.h>
- @interface SPSBMakePhoneCallManager ()<CNContactPickerDelegate, CNContactViewControllerDelegate> {
- __weak UIViewController *_vc;
- }
- @end
- @implementation SPSBMakePhoneCallManager
- - (instancetype)initWithController:(UIViewController *)vc {
- self = [super init];
- if (!self) return nil;
- _vc = vc;
- return self;
- }
- - (void)showHandleContactMenu {
- SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new;
- @weakify(self)
- [vc addTopView:[self creatActionSheetTipsView] height:0];
- [vc addActionWithTitle:@"直接呼叫" action:^{
- @strongify(self)
- [self makePhoneCall];
- }];
- [vc addActionWithTitle:@"添加到手机通讯录" action:^{
- @strongify(self)
- [self addToPhoneAddressBook];
- }];
- [_vc presentViewController:vc animated:false completion:nil];
- }
- - (void)makePhoneCall {
- NSMutableString *phone = [jxh_userDefaults() objectForKey:SPSBCustomerServicePhone];
- NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", phone];
- jxh_openUrl([NSURL URLWithString:callPhone], ^(BOOL success) {
- });
- }
- - (void)addToPhoneAddressBook {
- SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new;
- @weakify(self)
- [vc addActionWithTitle:@"创建新联系人" action:^{
- @strongify(self)
- [self createNewContact];
- }];
- [vc addActionWithTitle:@"添加到现有联系人" action:^{
- @strongify(self)
- [self updateContact];
- }];
- [_vc presentViewController:vc animated:false completion:nil];
- }
- - (void)createNewContact {
- CNContactViewController *contactVC = [CNContactViewController viewControllerForNewContact:[self initializeContact]];
- contactVC.delegate = self;
- UINavigationController *nav_contactVC = [[UINavigationController alloc] initWithRootViewController:contactVC];
- nav_contactVC.modalPresentationStyle = UIModalPresentationFullScreen;
- [_vc presentViewController:nav_contactVC animated:true completion:nil];
- }
- - (CNMutableContact *)initializeContact {
- // 创建联系人对象
- CNMutableContact *contact = [[CNMutableContact alloc] init];
-
- // 设置电话号码
- CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:[jxh_userDefaults() objectForKey:SPSBCustomerServicePhone]];
- CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:SPSBAppName value:mobileNumber];
- contact.phoneNumbers = @[mobilePhone];
-
- return contact;
- }
- - (void)updateContact {
- // 初始化CNContactPickerViewController
- CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
- contactPickerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
- // 设置代理
- contactPickerViewController.delegate = self;
- // 显示联系人窗口视图
- [_vc presentViewController:contactPickerViewController animated:true completion:nil];
- }
- - (void)updateContact:(CNMutableContact *)contact {
- // 创建联系人请求
- CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
- [saveRequest updateContact:contact];
- // 重新写入
- CNContactStore *store = [[CNContactStore alloc] init];
- [store executeSaveRequest:saveRequest error:nil];
-
- }
- - (void)addContact:(CNMutableContact *)contact {
- // 创建联系人请求
- CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
- [saveRequest addContact:contact toContainerWithIdentifier:nil];
- // 写入联系人
- CNContactStore *store = [[CNContactStore alloc] init];
- [store executeSaveRequest:saveRequest error:nil];
- }
- - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
- [_vc dismissViewControllerAnimated:true completion:nil];
-
- NSMutableArray *numbers = [contact.phoneNumbers mutableCopy];
- CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:[jxh_userDefaults() objectForKey:SPSBCustomerServicePhone]];
- CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:SPSBAppName value:mobileNumber];
- [numbers addObject:mobilePhone];
- CNMutableContact *newContact = [contact mutableCopy];
- newContact.phoneNumbers = numbers;
- CNContactViewController *contactVC = [CNContactViewController viewControllerForNewContact:newContact];
- contactVC.delegate = self;
- UINavigationController *nav_contactVC = [[UINavigationController alloc] initWithRootViewController:contactVC];
- nav_contactVC.modalPresentationStyle = UIModalPresentationFullScreen;
- [_vc presentViewController:nav_contactVC animated:true completion:nil];
- }
- - (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact {
- [viewController.navigationController dismissViewControllerAnimated:true completion:nil];
- }
- - (UIView *)creatActionSheetTipsView {
- UIView *tipsView = UIView.new;
- tipsView.backgroundColor = spsb_F5F5F5_color();
- UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(14) textColor:spsb_999999_color()limitWidth:jxh_screenWidth()];
- [titleLabel setAttributedStringWithText:@"服务时间为周一至周五9:00~12:00,13:30~17:30\n其他时间拨打可能无人接听" textAlignment:NSTextAlignmentCenter lineSpacing:4 lineHeight:0 paragraphSpacing:0 lineBreakMode:NSLineBreakByWordWrapping lines:2];
- [tipsView addSubview:titleLabel];
- [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(15);
- make.bottom.equalTo(-15);
- make.centerX.equalTo(0);
- }];
- return tipsView;
- }
- @end
|