Understanding sessions, messages, and conversations

So far, we have been looking at user interface-related aspects of iMessage apps only. When you're creating an iMessage app, your app will sooner or later have to send a message. To do this, we make use of MSMessage and MSSession. In addition to messages and sessions, there is the MSConversation class. These three classes together enable you to send messages, identify recipients in the conversations, and even update or collapse existing messages in the messages transcript.

When your extension is activated, willBecomeActive(with:) is called in the MessagesViewController. This method receives an MSConversation instance that you can use to send messages, stickers, and even attachments. More importantly, the conversation contains unique identifiers for participants in the conversation and the currently selected message.

The localParticipantIdentifier and remoteParticipantIdentifier properties, respectively, identify the current users of your app: the person who is sending messages with your app and the recipients of these messages. Note that these identifiers are unique to your app so you can't use this identifier to identify users across different iMessage apps. Also, these identifiers can change if the user uninstalls and then reinstalls your app. These identifiers are mostly meant to identify users in a group conversation, for example.

The selectedMessage property is only set if your extension was launched due to a user tapping on a message that was sent by your extension. This is especially useful if you're building an app that sends multiple messages that build upon each other. In Apple's 2016 WWDC talk, iMessage Apps and Stickers, Part 2, they demonstrated an ice cream designer app. Each user designs a part of the ice cream and the selectedMessage is used in combination with the MSSession to collapse old messages so the history does not get polluted with old pictures of ice creams. We'll see how to use this session in a bit. First, we'll have a look at MSMessage so we can compose messages.

Composing a message

The MSMessage class encapsulates all of the information that is contained inside of a message. When you initialize a message, you can initialize it with or without a session. If you instantiate a message with a session, other messages that are attached to this session will be collapsed to only show the message's summaryText. The summary is supposed to be a short, clear description of the message so that even when it's collapsed, the message still makes sense.

If possible, you should always aim to attach a url and accessibilityLabel to your messages, depending on the type of messages your app will send. If you're sending plain text messages like we'll do with The Daily Quote, you don't need an accessibility label since the screen reader can just read the text message. The url is mainly used by platforms such as Mac OS so URLs can still navigate to your content if needed. When you're sending plain text, this isn't really an issue.

Finally, if you're sending a message that has media attached to it, you should make use of the MSMessageTemplateLayout class. MSMessage has a layout property that can be set to an instance of MSMessageTemplateLayout. This layout template is highly configurable. You can assign an image or media to it, set a caption, an image title, a sub caption, and more. Messages will make sure that your layout looks good and is laid out nicely depending on the information you set.

If you set both a media URL and an image on your message layout, the image will be used and the other media is ignored. Any images you add should be 300x300 @3x. Avoid rendering text on the image as the scaling on different devices might degrade the quality of your image and render the text illegible. Instead, use the image title and image subtitle properties of the message layout.

Note that an MSMessage instance is always intended to either have some form of media associated with it or to be interactive.

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

In MessagesViewController, add the following:

func shareQuote(_ quote: Quote) { 
    guard let conversation = activeConversation 
        else { return } 
     
    conversation.insertText("(quote.text) - 
      (quote.creator)", completionHandler: nil) 
} 

If you run this code, you'll notice two things. First of all, if you have the app in expanded mode, the mode does not change to compact so we don't see the quote appear at all. Second, we don't actually send the quote. The user is always in control of which messages are sent and when they are sent. To fix the problem with quotes not appearing if we're in expanded mode, we need to add a single line of code to shareQuote(_:). After inserting the quote, add the following line:

dismiss() 

This will dismiss our extension entirely, allowing the user to focus on the quote and send it to the recipient.

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

If the user decided to not send your MSMessage instance, didCancelSending(_:) is called. You can use this method to clean up or respond to the cancel action in another, appropriate manner. Note that both the start and cancel methods are only called when you're working with MSMessage instances. If you're sending simple text like 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.219.249.210