呈现MFMessageComposeViewController / Understanding DispatchQueue.main.async

时间:2018-07-11 03:12:01

标签: ios swift viewcontroller cncontact mfmessagecomposeview

我要在一个人选择联系人后显示MFMessageComposeViewController。但是我收到lldb错误,并显示以下消息-

  

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序试图以模态形式显示活动控制器

这是我的代码:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    var recipients = [String]()

    //-- select contacts and present message compose view controller
    contacts.forEach { (contact) in
        for data in contact.phoneNumbers {
            let phoneNo = data.value
            recipients.append(phoneNo.stringValue)
        }

        //-- configure message view controller
        messageViewController.recipients = recipients
        messageViewController.body = "Testing Testing"

        //-- reload the view controller
        DispatchQueue.main.async {
             self.present(self.messageViewController, animated: true, completion: nil)
        }
    }
}

我对调度队列并不十分了解,所以我将对此进行更多的研究和线程化,但是如果有人愿意帮助我,将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题是您试图同时显示每个选定联系人的消息控制器。你不能那样做。您一次只能显示一个。您是否真的要显示多个消息控制器,每个联系人一个,还是所有联系人一个消息

由于您尝试显示多个MFMessageComposeViewController,因此,如果您想在外部for循环中进行一个呼叫,如下所示:

contacts.forEach { (contact) in
    for data in contact.phoneNumbers {
        let phoneNo = data.value
        recipients.append(phoneNo.stringValue)
    }
}

//-- configure message view controller
messageViewController.recipients = recipients
messageViewController.body = "Testing Testing"

//-- reload the view controller
DispatchQueue.main.async {
     self.present(self.messageViewController, animated: true, completion: nil)
}