簡體   English   中英

呈現MFMessageComposeViewController / Understanding DispatchQueue.main.async

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

我想在一個人選擇他們的聯系人后提出MFMessageComposeViewController 但是我收到以下消息的lldb錯誤-

***由於未捕獲的異常“ NSInvalidArgumentException”而終止應用程序,原因:“應用程序試圖以模態形式呈現活動控制器<Indexex.PortfolioSettingsViewController:0x1452000000>。”

這是我的代碼:

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

我對調度隊列的了解不是很深,所以我將對它和線程進行更多的研究,但是如果有人願意幫助我,將不勝感激。

問題是您試圖同時顯示每個選定聯系人的消息控制器。 你不能那樣做。 您一次只能顯示一個。 您是否真的要顯示多個消息控制器,每個聯系人一個,還是所有聯系人一個消息?

由於您嘗試呈現多個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