Sending a message

Once you have composed a message, you need to attach it to a conversation. To do so, you can use the activeConversation property, which is already present on your MSMessagesAppViewController. There are several methods on MSConversation that will insert a message. You can insert an instance of MSMessage if you have composed one. Alternatively, you can insert a sticker or you can simply insert some text.

To share a quote with The Daily Quote messages extension, this means that the text from the selected quote should be inserted into the conversation. Add the following implementation for shareQuote(_:) to implement the share feature:

func shareQuote(_ quote: Quote) {
  guard let conversation = activeConversation
    else { return }

  conversation.insertText("(quote.text) - (quote.creator)", completionHandler: nil)
}

If you run this code and try sharing a quote, two noteworthy things occur:

  • The extensions presentation mode does not change to compact after sharing a quote so the user doesn't see that the quote is shared.
  • When you change the app to compact mode manually, the quote isn't actually sent yet. Instead, a message is prepared for the user to send manually. Users are always in control of when a certain message is sent. Extensions are only allowed to prepare messages for users so they can decide for themselves whether the message should be sent.

To fix the problem with the extension not switching to compact mode after selecting a quote, you need to add a single line of code to shareQuote(_:). After inserting the quote, add the following line:

dismiss()

This will dismiss the extension entirely, allowing the user to focus on the quote and send it to the recipient. Alternatively, you could call requestPresentationStyle(_:) with a compact presentation style to change the current presentation style for your messages extension.

Once the user decides that they want to send the message you've composed on their behalf, they must manually send the message. Once this happens, didStartSending(_:conversation:) is called on your messages view controller. You won't be notified when this message was successfully sent to the recipient. This means that you can't make any assumptions in your app about whether the message was delivered to the recipient.

If the user decided to not send your MSMessage instance, didCancelSending(_:) is called. You can use this method to clean up or do something else in response to the cancel event. Note that both the start and cancel methods are only called when you're working with MSMessage instances. If you're sending a simple text, as The Daily Quote does, you won't be notified about these events at all.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.200.136