iOS开发笔记-107:Swift5 - 调用系统选择通讯录联系人,群发短信

import MessageUI
import ContactsUI
let picker = CNContactPickerViewController()
        // 2. 设置代理
        picker.delegate = self
        // 4. 弹出
        self.present(picker, animated: true, completion: nil)


//实现该代理方法,可以多选
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
        //选取通讯录后,先把之前的通讯录控制器先dismiss,之后的短信控制器才能弹出
        picker.dismiss(animated: true, completion: nil)
        
        var strA: Array = Array()
        contacts.forEach { contact in
            for number in contact.phoneNumbers {
                let phoneNumber: String = number.value.stringValue
                strA.append(phoneNumber)
            }
        }
        let controller = MFMessageComposeViewController()
        //短信的内容
        var introduceS: String = vcModel?.introduce ?? ""
        if introduceS == "" {
            introduceS = "我是" + (vcModel?.c_name ?? "") + kIntroduceString
        }
        controller.body = introduceS
        //联系人列表
        controller.recipients = strA
        //设置代理
        controller.messageComposeDelegate = self
        self.present(controller, animated: true, completion: nil)
    }

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true, completion: nil)
        //判断短信的状态
        switch result{
        case .sent:
            JJHUDManage.show("短信已发送")
        case .cancelled:
            JJHUDManage.show("短信取消发送")
        case .failed:
            JJHUDManage.show("短信发送失败")
        default:
            JJHUDManage.show("短信已发送")
            break
        }
    }

你可能感兴趣的:(iOS开发笔记-107:Swift5 - 调用系统选择通讯录联系人,群发短信)