从多个联系人的联系人发送短信失败
Sending SMS from Contacts for Multiple Contacts Fails
我试图达到与以下相同的结果:Sending SMS from Contacts Fails
- 发送短信或"App Invite"给联系人。
这个问题的解决方案有效,但它只允许您一次向一个人发送短信。 我想做的是一次向多个联系人发送一条短信。如果这很容易,请原谅我。在过去的 14 个小时里,我一直在编程,现在大多数事情对我来说都没有意义。
这是我的代码:
//MARK : VARIABLES
let contactPickerViewController = CNContactPickerViewController()
let messageViewController = MFMessageComposeViewController()
//MARK : VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
//-- set delegates equal to self
contactPickerViewController.delegate = self
messageViewController.messageComposeDelegate = self
}
//MARK : MFMESSAGECOMPOSE & CNCONTACTPICKERDELEGATE
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
self.dismiss(animated: true, completion: nil)
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
//-- select contacts and present message compose view controller
contacts.forEach { (contact) in
for data in contact.phoneNumbers {
let phoneNo = data.value
//-- configure message view controller
messageViewController.recipients = [phoneNo]
messageViewController.body = "Testing Testing"
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.present(self.messageViewController, animated: true, completion: nil)
})
}
}
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("cancelled")
}
在您的 for
循环中,您正试图为每个收件人显示一个 MFMessageComposeViewController
。这不会起作用,因为它会尝试同时呈现多个视图控制器。
您可以提交一份 MFMessageComposeViewController
,其中指定了所有收件人:
var recipients = [String]()
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"
self.present(self.messageViewController, animated: true, completion: nil)
我试图达到与以下相同的结果:Sending SMS from Contacts Fails
- 发送短信或"App Invite"给联系人。
这个问题的解决方案有效,但它只允许您一次向一个人发送短信。 我想做的是一次向多个联系人发送一条短信。如果这很容易,请原谅我。在过去的 14 个小时里,我一直在编程,现在大多数事情对我来说都没有意义。
这是我的代码:
//MARK : VARIABLES
let contactPickerViewController = CNContactPickerViewController()
let messageViewController = MFMessageComposeViewController()
//MARK : VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
//-- set delegates equal to self
contactPickerViewController.delegate = self
messageViewController.messageComposeDelegate = self
}
//MARK : MFMESSAGECOMPOSE & CNCONTACTPICKERDELEGATE
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
self.dismiss(animated: true, completion: nil)
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
//-- select contacts and present message compose view controller
contacts.forEach { (contact) in
for data in contact.phoneNumbers {
let phoneNo = data.value
//-- configure message view controller
messageViewController.recipients = [phoneNo]
messageViewController.body = "Testing Testing"
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.present(self.messageViewController, animated: true, completion: nil)
})
}
}
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("cancelled")
}
在您的 for
循环中,您正试图为每个收件人显示一个 MFMessageComposeViewController
。这不会起作用,因为它会尝试同时呈现多个视图控制器。
您可以提交一份 MFMessageComposeViewController
,其中指定了所有收件人:
var recipients = [String]()
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"
self.present(self.messageViewController, animated: true, completion: nil)