简体   繁体   English

呈现MFMessageComposeViewController / Understanding DispatchQueue.main.async

[英]Presenting MFMessageComposeViewController/Understanding DispatchQueue.main.async

I'm trying to present the MFMessageComposeViewController after a person selects their contacts. 我想在一个人选择他们的联系人后提出MFMessageComposeViewController But I'm getting the lldb error with the following message -- 但是我收到以下消息的lldb错误-

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <Indexex.PortfolioSettingsViewController: 0x1452000000>.' ***由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图以模态形式呈现活动控制器<Indexex.PortfolioSettingsViewController:0x1452000000>。”

Here's my code: 这是我的代码:

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)
        }
    }
}

I don't really understand that much about dispatch queue so I'm going to do way more research on that and threading but if anyone would be willing to help me out that would be greatly appreciated. 我对调度队列的了解不是很深,所以我将对它和线程进行更多的研究,但是如果有人愿意帮助我,将不胜感激。

The problem is that you are attempting to show a message controller for each selected contact, at the same time. 问题是您试图同时显示每个选定联系人的消息控制器。 You can't do that. 你不能那样做。 You can only show one at a time. 您一次只能显示一个。 Do you really want to show multiple message controllers, one for each contact, or one message with all of the contacts 您是否真的要显示多个消息控制器,每个联系人一个,还是所有联系人一个消息?

Since you try to present the multiple MFMessageComposeViewController , if you wanna single one call in outside for loop like below : 由于您尝试呈现多个MFMessageComposeViewControllerMFMessageComposeViewController ,如果您想在外部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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM