Is it possible to use SwiftUI in iMessage app?

I'm new to Xcode development so this question might be silly but when I create an iMessage project by selection "iMessage Application" template. And I want it to be standalone app for iMessage so that there is no iOS app as I don't see it useful in my case. I notice that there is only StoryBoard in the project and it looks a little bit different than iOS project. I watched Stanford iOS course 2020 so I'm use to SwiftUI and would prefer using it.
Post not yet marked as solved Up vote post of Lankinen Down vote post of Lankinen
1.5k views
Add a Comment

Replies

Yes, you have to

  1. Delete the storyboard file and delete the NSExtensionMainStoryboard property and add NSExtensionPrincipalClass that will be the class MessagesViewController and this your MSMessagesAppViewController.
  2. Add to your code @objc(MessagesViewController)
  3. In viewDidLoadMethod insert
// Get the UIKit view of your SwiftUI View
let child = UIHostingController(rootView: SwiftUIView())

self.view.addSubview(child.view)


// Set the place where your view will be displayed
let constraints = [

            child.view.topAnchor.constraint(equalTo: view.topAnchor),

            child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),

            child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),

            child.view.widthAnchor.constraint(equalTo: view.widthAnchor),

            child.view.heightAnchor.constraint(equalTo: view.heightAnchor)

]

view.addConstraints(constraints)

Hope it will help you !

  • I forgot, you have to add child.view.translatesAutoresizingMaskIntoConstraints = false after declaring child.

  • I forgot, you have to add child.view.translatesAutoresizingMaskIntoConstraints = false after declaring child.

Add a Comment