[iOS Ten day by day] Day 1: developing IMessage's Third-party Plugins
Source: Internet
Author: User
<span id="Label3"></p><p><p>This article describes an important update for IOS 10: the Messages app supports Third-party plug-ins. The author uses a small game as an example to illustrate the whole process of plug-in development starting from construction, to drawing the interface, and sending and receiving Messages.</p></p><p><p></p></p> <blockquote> <blockquote> <p>"ios days by day" is a series of blogs written by Shinobicontrols company that introduces new features that developers need to know about iOS 10, updated weekly. This series of translations (anthology Address) has been officially authorized. Cang Potato translation, Welcome correction:)</p> <p>Shinobicontrols provides IOS and Android developers with a high-performance, responsive UI control SDK, especially for Chart-side Controls. Official website: shinobicontrols.com Twitter: @shinobicontrols</p> </blockquote> </blockquote><p><p></p></p><p><p>Apple's official Messages has launched a very significant update in IOS 10, probably primarily wanting to some cards market share back from other IM giants, including Facebook Messenger, Wechat and Snapchat.</p></p><p><p></p></p><p><p>An important new feature is that users can use extensions developed by Third-party developers directly in Messages. This feature is based on the Extension technology introduced in IOS 8 and can be found in the articles written by Sam Davies in our previous Series. One of the great benefits of the Messages plugin is that it can exist independently of the app and not packaged with the parent app. Later this year IOS 10 will release a small Messages App Store with a bunch of plugins to choose From.</p></p><p><p></p></p><p><p>To demonstrate this exciting plugin feature, let's look at a simple example of this plugin that allows two of users to play a simplified version of the popular game Battleships. In order to make the layout of the constraint simple, we only consider the situation of the vertical Screen. To facilitate the download of this demo, I put it on github.</p></p><p><p>http://t.cn/RcLmZ0e</p></p><p><p></p></p><p><p>The rules of the game are this:</p></p><p><p></p></p> <ul class="list-paddingleft-2"> <ul class="list-paddingleft-2"> <li><p>Player A launches the game, arranging two "battleships" on the board and hiding Them.</p></li> <li><p>Another player, B, wants to guess where the battleship is.</p></li> <li><p>Player B wins if the location of two hidden warships is guessed, but if you guess 3 times, player B loses.</p></li> </ul> </ul><p><p></p></p><p><p><strong>Building projects</strong></p></p><p><p></p></p><p><p>Creating a new plugin project with Xcode is Easy. Just click File, New Project, and then select IMessage application in the WINDOW.</p></p><p><p></p></p><p><p>Give the project a name, and then the language chooses Swift (this series uses the Swift language example), and That's it. Because there is an auto-generated messagesextensiontarget, then the default info.plist with the necessary configuration (plug-in interface Storyboard and plug-in type, etc.), so as long as the project is run, Messages will be able to automatically identify our Plug-in.</p></p><p><p></p></p><p><p><strong>Change Display Name</strong></p></p><p><p></p></p><p><p>If you run the messagesextension target in the simulator, it will let you choose which app to run the plugin in. We choose Messages.</p></p><p><p></p></p><p><p>Running in the Messages.</p></p><p><p></p></p> <blockquote> <blockquote> <p>Messages should be able to see our plugin below the input box when it is Open. If you do not see it, you may need to click "applications" icon, then click on the 4 ellipse icon, select our plugin from the Inside.</p> </blockquote> </blockquote><p><p></p></p><p><p>There is nothing in it now, but we will soon change that. The most urgent thing now is to change the display name of our plugin: "messagesextension" (actually "messagesex ..."). The back was cut off). Below we click Target and change the name in the display name input box.</p></p><p><p></p></p><p><p>Change Display Name</p></p><p><p></p></p><p><p><strong>Board</strong></p></p><p><p></p></p><p><p>What we need to show is the 3x3 Chessboard. There are many implementations, I use UICOLLECTIONVIEW. In this tutorial, the drawing interface is not important, so the implementation details are no longer detailed.</p></p><p><p></p></p><p><p><strong>Data model</strong></p></p><p><p></p></p><p><p>To record the game itself and the state of the game, we define the following two Structures:</p></p><p><p></p></p> <blockquote> <p><p><span>struct<span> <span>gameconstants<span> <span>{</span> </span> </span> </span></span></p></p> <p><p><span>/// <span>total number of warships to be arranged</span></span></p></p> <p><p><span><span>static let<span> <span> <span>totalshipcount<span> = <span>2</span> </span> </span> </span> </span> </span></span></p></p> <p><p><span>/// <span>number of times the player B is allowed to fail</span></span></p></p> <p><p><span><span>static let<span> <span> <span>incorrectattemptsallowed<span> = <span>3</span> </span> </span> </span> </span> </span></span></p></p> <p><p>}</p></p> <p><p></p></p> <p><p><span>struct<span> <span>GameModel<span> <span>{</span> </span> </span> </span></span></p></p> <p><p><span>// <span>the location of the battleship</span></span></p></p> <p><p><span><span>let<span> <span>shiplocations<span>: <span>[<span>Int<span>]</span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p><span>/// <span>whether the game is over</span></span></p></p> <p><p><span><span>var<span> <span>iscomplete:bool</span> </span> </span></span></p></p> <p><p>}</p></p> </blockquote><p><p></p></p><p><p><strong>Messagesviewcontroller</strong></p></p><p><p></p></p><p><p>Messagesviewcontroller is the entry point for our plugin. It is a subclass of msmessagesappviewcontroller, which is equivalent to the root View Controller of the Messages plugin. The auto-generated template contains some methods for us to rewrite, such as the callback function that the user receives the message when the plugin is in the startup State. We're going to use some of these methods later.</p></p><p><p></p></p><p><p>The 1th note is that there are two possible presentation style after our plug-in is started:</p></p><p><p></p></p> <ul class="list-paddingleft-2"> <ul class="list-paddingleft-2"> <li><p>Compact</p></li> <li><p>Expanded</p></li> </ul> </ul><p><p></p></p><p><p>The Compact is the mode in which the user opens the plugin from the application tray and the plugin is displayed in the keyboard Area. Expanded give some breathing space, plug-ins occupy most of the Screen.</p></p><p><p></p></p><p><p>To keep the code neat, we use different view controllers to implement the two modes separately, and add these view controllers as Messagesviewcontroller's child view Controller.</p></p><p><p></p></p><p><p><strong>Several child View controllers</strong></p></p><p><p></p></p><p><p>This article does not take too long to describe the implementation details of these controllers, but focuses on the process of sending and receiving information, and how the game state and data Change. For specific implementations, Please read the source code on Github yourself.</p></p><p><p></p></p><p><p><strong>Gamestartviewcontroller</strong></p></p><p><p></p></p><p><p>Our plugin was in compact state when it was first Launched. This space is not enough to show the Game's chessboard, especially on the IPHONE. We can simply and rudely switch to the expanded state immediately, but Apple has warned against doing so, after all, it should give the user control over it.</p></p><p><p></p></p><p><p>so, Let's show a simple Welcome interface with a label and a Button. When you press the button, and then switch to the main screen of the game, the user can start to place the "battleship".</p></p><p><p></p></p><p><p><strong>Ship Location View Controller</strong></p></p><p><p></p></p><p><p>This view controller is the interface for player A to arrange a battleship.</p></p><p><p></p></p><p><p>We implement Gameboard's Oncellselection method to control the Cell's style: the cell with the battleship above is shown in green, and the blank is shown in Blue.</p></p><p><p></p></p><p><p>Shipslefttoposition returns to 0 o'clock, the end button becomes a bit. The Click event of this button is a ibaction method called completedshiplocationselection, which creates a new game model and then uses UIImage's extension To create a game board (we will first reset () the board, so the location of the battleship is Hidden-now is not the time to reveal the answer! )。 This one will be used when the message will be sent Later.</p></p><p><p></p></p><p><p><strong>Ship Destroy View Controller</strong></p></p><p><p></p></p><p><p>When player B clicks on the message in the conversation, we want him to see a slightly different view controller--a screen that will allow him to find a hidden battleship.</p></p><p><p></p></p><p><p>We still implement the Oncellselection method of the Chessboard. This time we'll mark the selected cell location with the position of player A ("hit Battleship") as green, or red if not hit.</p></p><p><p></p></p><p><p>At the end of the game, either because 3 lives were exhausted, or because two warships were found, we would record the data model accordingly, and then call back the Game's end Callback.</p></p><p><p></p></p><p><p><strong>Add Child Controller</strong></p></p><p><p></p></p><p><p>Back to our messagesviewcontroller, we can now put the controllers IN.</p></p><p><p></p></p> <blockquote> <p><p><span>Class<span> Messagesviewcontroller<span>: Msmessagesappviewcontroller<span> <span>{</span> </span> </span> </span></span></p></p> <p><p><span><span>override func willbecomeactive<span>(<span>with<span> conversation<span>: msconversation<span>)<span> <span>{</span> </span></span></span></span></span></span></span></span></p></p> <p><p><span><span>Configurechildviewcontroller<span>(<span><span>for: presentationstyle<span>,<span> with<span>: Conversation<span>) </span></span></span></span></span></span></span></span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p></p></p> <p><p><span><span>override func willtransition<span>(<span>to<span> presentationstyle<span>: Msmessagesapppresentationstyle<span>)<span> <span> {</span></span></span></span></span></span></span></span></span></p></p> <p><p><span><span>Guard Let <span>conversation<span> = <span>self<span>. <span> Activeconversation <span>Else<span> <span>{<span> <span>return<span> <span>}</span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></p></p> <p><p><span><span>Configurechildviewcontroller<span>(<span>for:presentationstyle<span>,<span> <span>with:conversation<span>)</span> </span></span></span></span></span></span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p>}</p></p> </blockquote><p><p></p></p><p><p>These two methods are inherited from msmessagesappviewcontroller, which reminds us that the plugin is started (such as being opened by the User) and is going to be transformed to another presentation Style. We use these two methods to configure the child view Controller.</p></p><p><p></p></p><p><p>The above method determines which child view controller we should present to the current User. If you are in compact mode, the start Game interface should be Displayed.</p></p><p><p></p></p><p><p>If you are in expanded mode, we need to determine whether a player or a B player. If the player B clicks on the message in the dialogue interface, then conversation.selectedmessage will not be nil, which means the game has already started, so we're going to show shipdestroyviewcontroller. otherwise, Show Shiplocationviewcontroller.</p></p><p><p></p></p><p><p><strong>Toggle Interface Mode</strong></p></p><p><p></p></p><p><p>At Gamestartviewcontroller Click on the "start game" button, we want the plugin to switch to expanded mode so that we can show the Board.</p></p><p><p></p></p> <blockquote> <blockquote> <p>In ' Creategamestartviewcontroller '.</p> <p><span>Controller<span>. <span> Onbuttontap<span> = <span>{</span> </span> </span> </span></span></p> <p><span><span>[<span>unowned <span>self<span>]<span> </span> </span> </span> </span> </span> in</span></p> <p><span><span>self<span>. <span> Requestpresentationstyle<span>(. <span> Expanded<span>)</span> </span> </span> </span> </span> </span></span></p> <p>}</p> <p> </p> </blockquote> </blockquote><p><p></p></p><p><p></p></p><p><p>Switch to expanded mode</p></p><p><p><strong>Create a "can update" message</strong></p></p><p><p></p></p><p><p>Before Messages, any new content-whether It's a new text message or an expression-would appear at the bottom of the conversation in the form of a new message, irrelevant to all the previous Messages.</p></p><p><p></p></p><p><p>however, This can cause a lot of trouble: for example, a game plugin that plays chess can cause a new message to be sent every step of the Way. Our ideal scenario should be that the updated message replaces the previous Message.</p></p><p><p></p></p><p><p>thankfully, Apple also thought of this, giving us a class Mssession--this class has no properties and no methods, just to update the Message.</p></p><p><p></p></p><p><p>When we send a message, we use this session to tell Messages to overwrite the same information as Before. The previous message is removed from the chat record, and the new information is inserted into the bottom section.</p></p><p><p></p></p><p><p><strong>Use name of contact person</strong></p></p><p><p></p></p><p><p>In recent years, Apple has been saying that it is a top priority to protect users ' privacy. This is true for the Messages framework: you don't get the identity of the user, you only get a different uuid for each device. That is, you cannot include the ID of the user who sent the message in the message, and then expect that the user receiving the message will be able to identify who started the message through this ID.</p></p><p><p></p></p> <blockquote> <blockquote> <p>In addition, you can only access the content of the message that the user clicked on, and cannot access the content of any other messages in the conversation (and the clicked message must also be sent from your plugin).</p> </blockquote> </blockquote><p><p></p></p><p><p>Msconversation This class has two attributes Localparticipantidentifier and remoteparticipantidentfiers, which can be used to display the names of both sides of a conversation. To add a prefix of $.</p></p><p><p></p></p> <blockquote> <blockquote> <p><span>Let <span>player<span> = <span>"$<span>\ (<span>conversation<span>. <span> Localparticipantidentifier<span>) "</span> </span> </span> </span> </span> </span> </span> </span></span></p> </blockquote> </blockquote><p><p><span></span></p></p><p><p>Put it in the message, Messages will parse the UUID, and then display the corresponding contact Name.</p></p><p><p></p></p><p><p>Show name of contact person</p></p><p><p><strong>Send and receive Application data</strong></p></p><p><p></p></p><p><p>The game state data is passed in the form of a URL. Your plugin on any phone should be able to parse the URL and show the relevant Content.</p></p><p><p></p></p><p><p>Another advantage of using URLs is that it also provides a fallback for MacOS Users. unfortunately, the Messages app on MacOS does not support plug-in Functionality. This is what the document Says:</p></p><p><p></p></p> <blockquote> <blockquote> <p>If you click on this message on macOS, the system will go to the Web browser to open the URL. So this URL should be directed to your own web service, based on the encode data in the URL to present a reasonable result for the User.</p> </blockquote> </blockquote><p><p></p></p><p><p>To build this url, we can use urlcomponents to combine a base URL and a bunch of urlqueryitems (all valid Key-value pairs).</p></p><p><p></p></p> <blockquote> <p><p><span>Extension <span>GameModel<span> <span>{</span> </span> </span></span></p></p> <p><p><span><span>func encode<span>()<span> , <span>URL<span> <span>{</span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>let <span>baseURL<span> = <span>"www.shinobicontrols.com/battleship"</span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>guard var <span>components<span> = <span>urlcomponents<span>(<span>string<span>: BaseURL<span>)<span> <span>else<span> <span>{ /c17> </span></span></span></span></span></span></span></span></span></span></span></span></span></p></p> <p><p><span><span>fatalerror<span>(<span>"Invalid base url"<span>)</span> </span> </span> </span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p></p></p> <p><p><span><span>var <span>items<span> = <span>[<span>urlqueryitem<span>] ()</span> </span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>//location of the battleship</span></span></p></p> <p><p><span><span>let <span>locationitems<span> = <span>shiplocations<span>. <span> Map<span> <span>{</span> </span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>location <span></span> </span> in</span></p></p> <p><p><span><span>Urlqueryitem<span>(<span>name<span>: <span>"ship_location"<span>,<span> value<span>: String<span>(<span> <span>location ))</span></span></span></span></span></span></span></span></span></span></span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p></p></p> <p><p><span><span>items<span>. <span> Append<span>(<span>contentsof<span>: Locationitems<span>)</span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>//game over</span></span></p></p> <p><p><span>Let complete <span> <span><span> = <span>iscomplete<span> <span>? <span> <span>"1"<span> : <span>"0"</span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>let <span>completeitem<span> = <span>urlqueryitem<span>(<span>name<span>: <span>"is_complete"<span>,<span> value<span>: Complete<span>)</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>items<span>. <span> Append<span>(<span>completeitem<span>)</span> </span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>components<span>. <span> Queryitems<span> = <span>items</span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span>Guard let <span>urls<span> = <span>components<span>. <span> URL <span>Else<span> <span>{</span> </span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>fatalerror<span>(<span>"Invalid URL components"<span>)</span> </span> </span> </span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p></p></p> <p><p><span><span>return<span> <span>URL</span> </span> </span></span></p></p> <p><p><span><span>}</span></span></p></p> <p><p>}</p></p> </blockquote><p><p></p></p><p><p>The final URL results are as follows: www.shinobicontrols.com/battleship?Ship_Location=0&Ship_Location=1&Is_Complete=0</p></p><p><p></p></p><p><p>Decoding is basically the opposite of this process: first get the url, take out each key-value pair, each corresponding value to build the Game's data model.</p></p><p><p></p></p><p><p><strong>Inserting information in a chat conversation</strong></p></p><p><p></p></p><p><p>Through the hard work ahead, we finally created this message, ready for the player to send to other players in the Conversation.</p></p><p><p></p></p> <blockquote> <p><p>Build a message and insert it into the conversation</p></p> <p><p><span>Func Insertmessagewith<span>(<span>caption<span>: String<span>,</span> </span> </span> </span></span></p></p> <p><p><span><span>_<span> Model<span>: GameModel<span>,</span> </span> </span> </span></span></p></p> <p><p><span><span>_<span> Session<span>: Mssession<span>,</span> </span> </span> </span></span></p></p> <p><p><span><span>_<span> Image<span>: UIImage<span>,</span> </span> </span> </span></span></p></p> <p><p><span><span>in<span> conversation<span>: msconversation<span>)<span> <span>{</span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>let <span>message<span> = <span>msmessage<span>(<span>session:session<span>)</span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p><span><span>let <span>template<span> = <span>msmessagetemplatelayout<span>()</span> </span> </span> </span> </span></span></p></p> <p><p><span><span>template<span>. <span> Image<span> = <span>image</span> </span> </span> </span> </span></span></p></p> <p><p><span><span>template<span>. <span> Caption<span> = <span>caption</span> </span> </span> </span> </span></span></p></p> <p><p><span><span>message<span>. <span> Layout<span> = <span>template</span> </span> </span> </span> </span></span></p></p> <p><p><span><span>message<span>. <span> URL<span> = <span>model<span>. <span> Encode<span>()</span> </span> </span> </span> </span> </span> </span> </span></span></p></p> <p><p></p></p> <p><p><span><span>//after we've Built this message, insert it into the conversation</span></span></p></p> <p><p><span><span>conversation<span>. <span> Insert<span>(<span>message<span>)</span> </span> </span> </span> </span> </span></span></p></p> <p><p>}</p></p> </blockquote><p><p></p></p><p><p>As I said earlier, this message is created with a session so that we can overwrite the message of the same session in the Conversation.</p></p><p><p></p></p><p><p>In order to modify the appearance of the message, we need to use Msmessagetemplatelayout. It allows us to modify a series of properties of the message, in this case the main use of caption (text) and image (image).</p></p><p><p></p></p><p><p>After modifying the appearance of the message, Configuring the session and URL properties, we can finally insert the message into the Conversation. The last line of code will put the message into the Messages input box. Note: We do not have permission to send this message directly-it can only be placed in the input box.</p></p><p><p></p></p><p><p><strong>It's Over.</strong></p></p><p><p></p></p><p><p>After inserting this message, there is no need for our plugin to stay Idle. The user can turn it off manually, but in order for them to experience a little better, we call this line of code and end up Messagesviewcontroller life:</p></p><p><p></p></p> <blockquote> <blockquote> <p><span>Self<span>. <span> Dismiss<span>()</span> </span> </span></span></p> </blockquote> </blockquote><p><p><span></span></p></p><p><p></p></p><p><p><strong>Extended Reading</strong></p></p><p><p></p></p><p><p>Thank you for reading this long article, hoping to give you a glimpse of the powerful features of the IOS one or two Message app.</p></p><p><p></p></p><p><p>There are a few minor problems with the current Beta: IOS simulator startup Messages app is slow and sometimes it's not loaded-i often need to manually restart my plugin from the Messages application tray. And the Messages framework is very "verbose": the log is almost the most extreme. Of course, These issues will be resolved after IOS 10 finishes beta, but you still need a pair of eyes in this state, from a lot of debug information to find content related to your plugin, such as AutoLayout constraint Conflict.</p></p><p><p></p></p><p><p>If you still want to continue to explore, I recommend you watch this WWDC video, you can also look at Apple's official example project: There are many interesting tips to learn, such as how to gracefully parse the URL.</p></p><p><p></p></p><p><p>If you have any questions or comments, we would be happy to welcome your FEEDBACK. You can send me a tweet @sam_burnstone, or follow @shinobicontrols updates on the latest news and the IOS day of day Series. Thanks for reading!</p></p><p><p></p></p><p><p>[iOS Ten day by day] Day 1: developing IMessage's Third-party Plugins</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service