Kodeco Forums

iOS 10 Screencast: Sending Custom Messages in iMessage

Learn how use the new Messages framework in iOS 10 to construct and send completely custom messages by creating a simple game right inside iMessages.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4954-ios-10-sending-custom-messages-in-imessage

If you have downloaded Beta 2, will need to replace the following line:

conversation.insert(message, localizedChangeDescription: "Updated Drawing!") { (error) in

with:

conversation.insert(message) { (error) in

Current documentation https://developer.apple.com/reference/messages/msconversation

Beta 1 doc https://developer.apple.com/reference/messages/msconversation/1648190-insert

I understand beta 2 has only just come out.

1 Like

Thanks for pointing that out @mrpaulb - such is the challenge of producing content really quickly.

Rest assured that the accompanying book iOS 10 by Tutorials will be updated with the GM release. It’s more difficult to re-record videos :slight_smile:

Understood, is great that you guys get tutorial out in such a short timespan. My motivation was only to help other students. Content is very good, although quite a lot to take in at once, I may have to watch, follow along multiple times.

Paul

1 Like

Thanks for your kind words, and for recording your experience in the forums to help others - precisely what they’re for.

I hope you enjoy the upcoming screencasts, I’ve been working on some cool stuff - and hope you’ll agree :smiley:

1 Like

I downloaded the materials and in the MessagesViewController, the compiler keeps trying to replace calls of “presentViewController(forConversation:,withPresentationStyle:)” with " …(conversation:, animated:)" it doesn’t seem to recognize the extension

Hi @prefpkg,

This project was written with an early beta of Xcode 8 and iOS 10. I suggest that you check out the project that accompanies the iOS 10 by Tutorials book, which will be updated with the latest Xcode when it is released.

sam

I have been playing with a custom iMessage app and I am trying to ad a CNContactPickerViewController as a view in my app. For some reason when the controller comes up in expanded mode, the search bar and top part of controller is hidden behind the iMessage Nav bar. How can i constrain the controller properly so that the top of CNContactPickerViewController starts at the bottom of Nav Bar?

@digimarktech I’m pretty sure that I read somewhere in the release notes for iOS 10 betas that this was a known issue and the suggested workaround was to update the constraint on the content to add a margin at the top.

Not a very nice answer I appreciate, but I’m pretty sure that this was what Apple was recommending to do…

sam

@samdavies gotcha! I guess I must have missed that. With that being said how do you go about adding a constraint to something that doesn’t exist in the storyboard? I am for the most part using the same methodology that the wenderPic app is using as far as adding a child view controller, creating constraints around it, using didMove to parentviewcontroller and using the presentViewController function in the willtransition and willBecomeActive to display the child controller. I was under the impression that as long as I instantiate the controller the same way my other view controllers are being called, the constraints defined in my presentViewController method would take place for all views. The storyboard for my CNContactPickerViewController is blank as I am creating the controller programmatically. Do I add constraints within the same section that I define the controller? When I tried that I received all types of constraint errors and illegal operation messages. I can confirm that if I create a basic label on the same viewcontroller that I am trying to add my contact picker to, and I constrain it within the storyboard, when that controller comes up in expanded mode, it does show up properly. This confirms that if I add constraints, everything should work. It’s just a matter of figuring that out programmatically…

@digimarktech

Adding constraints programmatically got a whole lot easier in iOS 9.

The class to lookup is NSLayoutConstraint, and you should use NSLayoutAnchors in most cases.

Pinning a subview subview to all four edges of its containing view view can be done as follows:

view.addSubview(subview)
subview.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
  subview.leftAnchor.constraint(equalTo: view.leftAnchor),
  subview.rightAnchor.constraint(equalTo: view.rightAnchor),
  subview.topAnchor.constraint(equalTo: view.topAnchor),
  subview.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

The really important thing to remember is that you need to set translatesAutoresizingMaskIntoConstraints to false for views that you create in code when you’re adding constraints.

If you need more info on this, check out the auto layout video series, or some of the other layout tutorials on raywenderlich.com.

sam

I added constraints just like that in my View Did load method. I noticed that from the example in the wenderPic app. Here is my code:

let contactPicker = CNContactPickerViewController()

    view.addSubview(contactPicker.view)
    contactPicker.view.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        
        contactPicker.view.leftAnchor.constraint(equalTo: view.leftAnchor),
        contactPicker.view.rightAnchor.constraint(equalTo: view.rightAnchor),
        contactPicker.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 200),
        contactPicker.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        
        ])
    
    contactPicker.didMove(toParentViewController: self)
    
    present(contactPicker, animated: true)

Even after setting this up in my viewDidLoad method, my contact picker still shows up behind the nav bar. I tried adding a breakpoint and this code does get called yet the constraint is not being applied…

@digimarktech - hrm - not sure what’s going on there.

You could try using the view.topAnchor instead of the top layout guide, and messing around with the constant to see if it has any effect.

Other than that I’m out of ideas without investigating a project myself.

Sorry

sam