IOS 10 Create IMessage App

Source: Internet
Author: User
Tags home screen

Original author of Australian 19-year-old--davis Allie----address

When the Mid-Autumn Festival, to the outside of the program Ape!

Sao years, write an expression on your own.


Dou-seal.svg.png

First. Introduced

With the release of iOS10 , Apple has opened the messages app to developers, and developers can now create their own IMessage appswith various types that can inline iMessage content. Contains a simple emoticon package to a complex interface that can be interacted with.

In this tutorial, I'll introduce you to the new Framework--messages framework and show you how to create your own imessage Apps.

The premise of this tutorial is that you run Xcode8 in OS X El Capitan or above and have some experience in iOS development, and if you are just beginning to learn iOS development, you can first learn this course iOS from Scratch with Swift; In this series, you'll learn how to start developing an in-depth article and tutorial on the iOS platform.

1. Eco-System (Ecosystem)
The IMessage app ecosystem starts with a complete app Storethat users can access through the message application. This app store is completely separate from the previous store and will only show the imessage-related app .

The presence of IMessage apps extends the impression of iOS apps to some extent, such as emoji packs (no emoticons) and custom keyboards, the most important of which is that the IMessage App Store exists only in the system messages , you create a imessage app that doesn't need to appear on your phone's home screen like the iOS app, imessage app creates an extended, effective blank and invisible iOS app. However, if you want to develop an app that includes iOS and imessage, you can create a imessage extension like any other type of extension.

Finally, the IMessage app is only available on iOS, but its content can be displayed on MacOS and WatchOS, and WatchOS 3 of users can view the most recently used emoticons and send these emoticons directly to the contact from Apple's watch.

Second. Basic Emoticons Pack

To make it quick and easy for developers to create a imessage emoticon, Xcode provides a template without any code! This will be a great tool for artists with no programming knowledge to create an emoticon package.

Using the new Messages framework to create an emoticon, you can create small, moderate, large emoticons, this size, suitable for all emoji packs. You only need to provide the maximum image size for each emoticon in your emoticon pack, and if you need to scale down on some devices, the system will do it for you.

Although there is no strict limit, Apple's recommended emoticons size:

1. Small: 100 x 100 pt @3x scale (300 x 300 pixel image)2. Medium: 136 x 136 pt @3x scale (378 x 378 pixel image)3. Large: 206 x 206 pt @3x scale (618 x 618 pixel image)

There are also some other limitations to the size of the emoticon pack:

1. 文件中images不可以大于500kb;2. iamge不可以小于100 x 100 pt (300 x 300 pixels).3. iamge不可以大于206 x 206 pt (618 x 618 pixels).4. 图片格式必须是 PNG, APNG, JPEG, GIF ;

Open Xcode, create a new project, select IOS > Application, and you'll see iMessage application and sticker Pack application option, in the first example , select sticker Pack application :


Paste_image.png

If you keep doing it and you want some expression, you can download--tutorial GitHub repo from here.

Open the project and you will find a stickers.xcstickers in the left sidebar of Xcode, which contains the icon and expression of the IMessage app, which you can drag onto your project later sticker The new sticker Sequence in the Pack folder;


Paste_image.png

If you want to add a dynamic emoticon Then you can add a set of images, you could click on the sticker Pack folder, choose Add Assets > New sticker Sequence This option, in this You can set it again in a smiley bag;

Note Be sure to follow the steps to add a picture


3.png

Finally, if you want to change the default name and size of the emoticon package, you can change it in the right Attributes Inspector ;


4.png Test your emoticon pack

Test emoticons, CMD + R , or click the Run button in the top left corner, and the simulator will pop up the following menu:


5.png

Select Message and click the Run button, the simulator shows the message app, click the App Store button , which is the button near the input box;


6.png

Your emoticons will show up in the front, and you'll find two expressions to use and send.


7.png

Click on any expression, it will be added to the current information,


8.png

As you can see, in iOS10 you are able to quickly and easily create emoticons and do not need any code;

Third Custom Label app

In the user's need, the basic Emoticons app provides a template that may not fully meet the requirements; For this reason, Apple also provides a way for you to create a more complex label application; If you want to do this app together, create a project Customstickerpack , this time choose iMessage application template;

After creating this empty project, you will see a list of files similar to the iOS app, but with one more messagesextension folder, the top customstickerpack folder contains Assets.xcassets and info.plist files, it is important that you provide all the right size application icons in this assets.xcassets directory, because iOS will also use it, For example, the user's store uses the settings.


9.png

We focused on the messageextension file,

MessagesViewController.swift : iMessage app的程序入口;2. MainInterface.storyboard: 可视化操作;3. Assets.xcassets: 图片集合;4. Info.plist : 配置一些扩展信息;

In our example, we create the Msstickerbrowserviewcontroller class;

Open Messagesviewcontroller.swift , you will find it is Msmessagesappviewcontroller subclass, and then look up to the parent class is Uiviewcontroller, which means that the IMessage app complies with the Uikit standard;

This Msmessagesappviewcontroller class provides a number of callback functions that you can override to further customize the functionality of the application, but we don't need this first; Msstickerbrowserviewdatasource Agreement;

class MessagesViewController: MSMessagesAppViewController, MSStickerBrowserViewDataSource { ...}

Before we can show our expressions, we need to add files to our project and load them, drag and drop the previously used picture folders into the project, and make sure they are added to the messagesextension target, Because this path will load them as URLs, it is easier to use when loading emoticons;

Next, create a mssticker array in Messagesviewcontroller to store our emoticons, and write a method to always load them locally;
The following code:

var stickers = [mssticker] () func loadstickers  () {for i in 1...2 {//syntax changed if let url = bundle.main.url ( Forresource:  "sticker \ (i)", Withextension:  "PNG") {do {let sticker = try MSSticker ( Contentsoffileurl:url, localizeddescription: print (Error)}}}           

Then add createstickerbrowserto this class, we initialize a Msstickerbrowserviewcontroller as the root view, and set the width height constraint:

Funccreatestickerbrowser () {let Controller = msstickerbrowserviewcontroller (stickersize:. Large) Addchildviewcontroller ( Controller) View.addsubview (controller.view) //syntax changed Controller.stickerBrowserView.backgroundColor = uicolor.blue Controller.stickerBrowserView.dataSource = self view.topAnchor.constraint (Equalto: Controller.view.topAnchor). isActive = true view.bottomAnchor.constraint (Equalto: Controller.view.bottomAnchor). isActive = true view.leftAnchor.constraint (Equalto: Controller.view.leftAnchor). isActive = true view.rightAnchor.constraint (Equalto: Controller.view.rightAnchor). isActive = true}      

Then we need to implement the Proxy method that Msstickerbrowserviewdatasource must implement;

func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int { return stickers.count}func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker { return stickers[index]}

Finally, all the code has been completed, replacing the code in viewdidload :

override func viewDidLoad() { super.viewDidLoad() loadStickers() createStickerBrowser()}

Run the app like before, and once everything is loaded, you should see an identical screen in addition to the blue background, except for the blue color background;


10.png

In this tutorial, we just loaded the images in the application, but the good thing is that we can customize the label application, but could you load the Web server? Before showing your Msstickerbrowserviewcontroller , you can do it by using a different view controllers . Let's go one step further!

Custom Apps

In the last section, we will create a very basic imessage application to create a unique message.;

If you would like to continue with this goal, open Xcode to create a Messageapp, open maininterface.storyboard, delete the default label, add a Stepper and a button, such as:


11.png

Note that in order for the view location of your imessage app to appear correctly on all devices, you need to add a layout constraint, in which case I have set the stepper in the middle of the view, the button on the lower side of the view;

Then, select Stepper to open the Attributes Inspector, changing the minimum maximum value to 0 and 10:


12.png

Then open the messagesviewcontroller.swift in Assistant editor and link it up;

@IBOutlet weak var stepper: UIStepper!@IBAction func didPress(button sender: AnyObject) {}

Next, the time to write the code, the first step I introduce some of the classes used:

MSConversation: 代表了当前打开的对话。可以使用这个类来操作一些步骤,例如通过插入消息或当前选择的信息;2. MSMessage: 代表单个消息,判断是否由你插入对话或已经存在的对话;3. MSMessageTemplateLayout: 创建一个消息气泡,如, 里边有许多属性和空间来提供给你来自定义message;

13.png

Specifically reminded that the upper left corner of the space is showing your icon, all properties are optional, providing any caption string will get rid of the bottom part of the layout.

In messagesviewcontroller.swift , modify the following code:

In this method, we display the value of the current stepper to the Circle label, and then place the label in the UIImage object so that we can capture it in the message;

Func createimageformessage ()UIImage? {Let background =UIView (frame:CGRect (x:0, Y:0, Width:Height:)) Background. BackgroundColor =Uicolor. White Let label =UILabel (frame:CGRect (x:X, Y:, Width:Max, Height:)) label. Font =Uifont. Systemfont (ofsize:56.0) Label. BackgroundColor =Uicolor. Red Label. TextColor =Uicolor. White Label. Text ="\ (Int (stepper.value))" label. TextAlignment =. Center Label. Layer. Cornerradius = Label. Frame. Size. width/2.0 Label. Clipstobounds =True background. Addsubview (label) background. Frame. Origin =Cgpoint (X:view. Frame. Size. width, Y:view. Frame.size.height) viewuigraphicsbeginimagecontextwithoptions (Background.frame.size, false, uiscreen.main.scale) Background.drawhierarchy (in:background.bounds, Afterscreenupdates: true) Let image = uigraphicsgetimagefromcurrentimagecontext ()  Uigraphicsendimagecontext () Background.removefromsuperview () return image}            

Next in didpress (button:) is changed to the following code:

First create the layout of the message, and set the image and caption, then we create the msmessage object and insert it into the session;

 @IBAction  Func didpress (Button Sender:anyobject) { if let image = Createimageformessage (), let conversation = activeconversation {let layout = msmessagetemplatelayout () layout.image = Image layout.caption =  "Stepper Value" let message = msmessage () message.layout = Layout Message.url = Span class= "Hljs-type" >url (string:  "Emptyurl") conversation.insert (message, Completionhandler: {(error) in print (Error)})}}   

Note that in imessage, custom message must be set to layout and URL, which is a link to some Web page content, so MacOS users can see the content of imessage, in this example, We just created a simple character url;

Finally we insert this message into the currently active session, calling this method does not send information, instead it puts your information in the user's input field so that they can be sent.

Run the app again and you'll see the following screen:


14.png

You click the Create message button and you will find the message position in the middle of the input box and can be sent;


15.png

Summarize

In this tutorial, I will introduce you to the new Messages frameworkin IOS10, which allows you to create emoticons and imessage applications, and we rewrite the underlying classes, including: Msstickerbroeserviewcontroller, Msconversation, Msmessage, Mstemplatemessagelayout;

This framework many APIs to develop your own imessage APP, further, I would recommend you to view Apple's documentation-the Messages Framework Reference.



Wen/mugworts (author of Jane's book)
Original link: http://www.jianshu.com/p/8d8844f621b2
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

IOS 10 Create imessage App

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.