MMSService
A class that provides an interface for performing MMS operations.
Declaration
final class MMSServiceMentioned in
Overview
Use the TelephonyMessagingSession property mmsService to get an instance of this class. You can then monitor the incomingMessageNotifications asynchronous sequence to receive notification of incoming messages, and the sendMessage(_:) method to send outgoing messages.
The following example shows how to send an message consisting of a text portion and an image attachment.
import TelephonyMessagingKit
let service = TelephonyMessagingSession.shared.mmsService
let cellularServices = try session.cellularServices
let cellularServiceID = cellularServices[0].id
guard service.isViable(for: cellularServiceID) else {
return
}
var recipients: [MMSHandle] = []
for phone in recipientPhoneNumbers {
recipients.append(MMSHandle(phoneNumber: phone))
}
var parts: [MMSPartContent] = []
let content1 = MMSPartContent(data: text.data(using: .utf8)!,
contentType: UTType.plainText,
contentID: UUID().uuidString,
disposition: .inline,
fileName: "")
let content2 = MMSPartContent(data: imageData,
contentType: UTType.jpeg,
contentID: UUID().uuidString,
disposition: .attachment,
fileName: "Image_1.jpg")
parts.append(content1)
parts.append(content2)
let content = MMSContent(parts: parts, recipients: recipients, subject: "")
let msgID = MMSMessageID(rawValue: SAMPLE_MMS_MESSAGE_ID)
let message = MMSMessage(cellularServiceID: cellularServiceID,
messageID: msgID,
content: content)
Task {
do {
try await service.sendMessage(message:message)
// Handle MMS send success.
} catch {
// Handle MMS send failure.
}
}
To receive messages, iterate over the incomingMessageNotifications asynchronous sequence, and retrieve messages from each notification as it arrives, as shown below:
import TelephonyMessagingKit
let service = TelephonyMessagingSession.shared.mmsService
let incomingMessageNotifications = try service.incomingMessageNotifications
Task {
for await notification in incomingMessageNotifications {
let receivedMessage = notification.message
// Process receivedMessage.
}
}