Contents

SMSService

A class that provides an interface for performing SMS operations.

Declaration

final class SMSService

Mentioned in

Overview

Use the TelephonyMessagingSession property smsService to get an instance of this class. You can then monitor the incomingMessageNotifications asynchronous sequence to receive notifications of incoming messages, and the sendMessage(_:) method to send outgoing messages.

The following example shows how to send an SMS message with the first service found in the session’s cellularServices array.

import TelephonyMessagingKit

let service = TelephonyMessagingSession.shared.smsService

let cellularServices = try session.cellularServices
let cellularServiceID = cellularServices[0].id

guard service.isViable(for: cellularServiceID) else { return }

let message = SMSMessage(
    cellularServiceID: cellularServiceID,
    handle: SMSHandle(phoneNumber: SAMPLE_PHONE_NUMBER),
    messageID: SMSMessageID(rawValue: SAMPLE_MESSAGE_ID),
    content: SMSContent(body: "Hello there.")
)

try await service.sendMessage(message)

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.smsService

let incomingMessageNotifications = try service.incomingMessageNotifications
Task {
    for await notification in incomingMessageNotifications {
        let receivedMessage = notification.message
        // Process receivedMessage.
    }
}

Topics

Determining service viabililty

Sending messages

Receiving messages

Handling critical state changes

Reporting spam

Handling errors

See Also

Using Short Message Service (SMS)