- func createStickerBrowser() {
- let controller = MSStickerBrowserViewController(stickerSize: .large)
- addChildViewController(controller)
- view.addSubview(controller.view)
- 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
- }
Next, we need to implement the required MSStickerBrowserViewDataSource methods to provide the necessary sticker information. To do so, add the following methods to your code:
- func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
- return stickers.count
- }
- func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
- return stickers[index]
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- loadStickers()
- createStickerBrowser()
- }
![]() |
4. Custom Applications
In the last part of this tutorial, we are going to create a very basic iMessage application in order to create a unique message.
If you want to follow along, open up Xcode and create another iMessage Application called MessageApp. Open up your MainInterface.storyboard file and in the view controller shown, remove the default label and add a stepper and button as shown:
![]() |
Next, open up the Attributes inspector with your stepper selected and change its minimum and maximum values to 0 and 10 respectively:
![]() |
- @IBOutlet weak var stepper: UIStepper!
- @IBAction func didPress(button sender: AnyObject) {
- }
- MSConversation: represents the currently open conversation. You can use this class in order to manipulate the conversation transcript, for example by inserting messages or getting the currently selected message.
- MSMessage: represents a single message, whether created by you to insert into the conversation or already existing in the conversation.
- MSMessageTemplateLayout: creates a message bubble for you to display your custom message in. As shown in the below image, this template layout has many properties and places for you to put your own content in order to customise your messages.
![]() |
Still in your MessagesViewController.swift file, add the following method to your MessagesViewController class:
- func createImageForMessage() -> UIImage? {
- let background = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
- background.backgroundColor = UIColor.white()
- let label = UILabel(frame: CGRect(x: 75, y: 75, width: 150, height: 150))
- 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)
- view.addSubview(background)
- UIGraphicsBeginImageContextWithOptions(background.frame.size, false, UIScreen.main().scale)
- background.drawHierarchy(in: background.bounds, afterScreenUpdates: true)
- let image = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- background.removeFromSuperview()
- return image
- }
Next, replace your didPress(button:) method with the following code:
- @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 = URL(string: "emptyURL")
- conversation.insert(message, completionHandler: { (error: NSError?) in
- print(error)
- })
- }
- }
Please note that for iMessage to process your custom message correctly, you must set a layout and url for your message. This url is intended to link to a web page of some sort where macOS users can also view your custom iMessage content. For this simple example, however, we just created a basic url from a string.
Lastly, we insert the message into the current active conversation. Calling this method does not actually send the message, though—instead it puts your message in the user's entry field so that they can press send themselves.
Build and run your new app once again, and you will see an interface similar to the following:
![]() |
![]() |
In this tutorial, I introduced you to the new Messages framework in iOS 10, which allows you to create sticker packs and applications to integrate with iMessage. We covered the basic classes you need to be aware of, including MSStickerBrowserViewController, MSConversation, MSMessage, and MSTemplateMessageLayout.
The Messages framework provides APIs to give you a large amount of control over your iMessage apps. For further reading, I would recommend checking out Apple's Messages Framework Reference.
Written by Davis Allie
If you found this post interesting, follow and support us.
Suggest for you:
iOS 10 Projects: Build Amazing Apps with Apple's Newest iOS
iOS 10 Message Extension App Reskinning without Coding
iOS 10 & Swift 3: From Beginner to Paid Professional
Swift 3 and iOS 10 The Final Course Learn to Code like a Pro
The Complete iOS 10 Developer - Build Real Apps with Swift 3






No comments:
Post a Comment