// // SPSBMakePhoneCallManager.m // 我的社保 // // Created by shanp on 2021/5/22. // #import "SPSBMakePhoneCallManager.h" #import "SPSBActionSheetViewController.h" #import "SPSBUIGeneralHeader.h" #import "SPSBKeyProfile.h" #import #import @interface SPSBMakePhoneCallManager () { __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