Explore a possibility of sending group messages through iMessage

What is iMessage

iMessage is an instant messaging software from Apple that is available on Apple devices such as iPhone, iPad, Mac and Apple Watch. iMessage can send various types of messages such as text, pictures, videos, voice messages and emoticons through Wi-Fi or cellular networks. iMessage also supports sending SMS and MMS. Compared with ordinary SMS and MMS, iMessage messages can be transmitted faster and more securely. At the same time, it can realize end-to-end encryption to protect user privacy. iMessage can also create group chats and send locations and other functions to facilitate communication and collaboration between users and friends, family and colleagues.

When iMessages are sent via carrier SMS

iMessages may be sent via carrier SMS in the following situations:

  • When iMessage cannot send messages through the network connection, the system will automatically switch to using carrier SMS to send messages.
  • When the content of the message sent by the user exceeds the character limit of the operator's short message, iMessage will divide the message into multiple short messages and send them, which will consume the user's short message charges.
  • When the recipient of the message sent by the user is not an Apple device or the iMessage function is not enabled, iMessage will convert the message to the operator's SMS and send it.

In these cases, iMessage will automatically switch to carrier SMS delivery to ensure that messages are delivered to recipients accurately and in a timely manner.

Several Ways to Send Group Messages Using iMesage

1. Use iMessage groups

Group messages can be sent via iMessage. In iMessage, you can create a group, add multiple contacts to it, and send a message to the entire group. All users need to be added to the group in advance.

2. Batch sending

To send in batches through MFMessageComposeViewController, you need to click the send button to trigger the sending logic. Automation can be achieved by automatically triggering the button click through UITest

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
    
    

    // 发送批量短信的函数
    func sendBulkSMS() {
    
    
        // 检查设备是否能发送文本消息
        if MFMessageComposeViewController.canSendText() {
    
    
            let messageComposeVC = MFMessageComposeViewController()
            messageComposeVC.messageComposeDelegate = self

            // 设置收件人列表
            let recipients = [
                "+1234567890",
                "+0987654321",
                // ...更多电话号码
            ]
            messageComposeVC.recipients = recipients

            // 设置消息内容
            messageComposeVC.body = "这是一条批量发送的短信"

            // 显示消息视图控制器
            self.present(messageComposeVC, animated: true, completion: nil)
        } else {
    
    
            print("设备不支持发送文本消息")
        }
    }

    // MFMessageComposeViewControllerDelegate的代理方法
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    
    
        // 处理发送结果
        switch result {
    
    
        case .sent:
            print("消息已发送")
        case .cancelled:
            print("用户取消发送")
        case .failed:
            print("消息发送失败")
        @unknown default:
            print("未知错误")
        }
        
        // 关闭消息视图控制器
        controller.dismiss(animated: true, completion: nil)
    }
}

You can customize the sending number information as the following code

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
    
    
    func sendBulkSMS() {
    
    
        let phoneNumbersAndMessages: [String: String] = [
            "+1234567890": "你好,这是发送给+1234567890的自定义消息。",
            "+0987654321": "你好,这是发送给+0987654321的自定义消息。",
            // 添加更多电话号码及其对应的自定义消息
        ]

        for (phoneNumber, message) in phoneNumbersAndMessages {
    
    
            if MFMessageComposeViewController.canSendText() {
    
    
                let messageComposeVC = MFMessageComposeViewController()
                messageComposeVC.messageComposeDelegate = self
                messageComposeVC.recipients = [phoneNumber]
                messageComposeVC.body = message

                self.present(messageComposeVC, animated: true, completion: nil)
            } else {
    
    
                print("设备无法发送短信。")
            }
        }
    }

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    
    
        controller.dismiss(animated: true, completion: nil)
    }
    // 其他代码,例如sendBulkSMSButtonTapped方法...
}

Guess you like

Origin blog.csdn.net/sinat_15735647/article/details/130461426