SwiftUI Integration

Adding to a Navigation Hierarchy

When integrating in a SwiftUI view hierarchy, ensure that the UIViewController that is returned by the UIViewControllerRepresentable makeUIViewController(context:) method is a UINavigationController containing the desired SDK UIViewController (for example, the Messaging UIViewController).

Also, ensure that the SwiftUI View conforming to UIViewControllerRepresentable is not contained within a NavigationView.

In addition, depending on the surrounding view hierarchy, SwiftUI may not honor an edgesIgnoringSafeArea(_:) request. This can happen, for example, if the view is inside a container that respects the screen's safe area (which is the case of the SDK views). In that case you may need to apply edgesIgnoringSafeArea(_:) to the container instead.

Following the above guidance will avoid issues such as navigation bar titles and buttons not appearing as expected, as well as unexpected behavior with SDK views frame on UI state changing (i.e. keyboard display, full screen style presentation).

The code below demonstrates how to display the Messaging UIViewController from a SwiftUI View.

struct ContentView: View {
  var body: some View {      // Do not wrap the below view in a NavigationView      MessagingView()          .edgesIgnoringSafeArea([.bottom, .top])  }}
struct MessagingView: UIViewControllerRepresentable {
  func makeUIViewController(context: Context) -> UIViewController {
      var viewController = UIViewController()      let engines = [ChatEngine.engine()]      let configs = [ChatConfiguration()]
      do {          viewController = try Messaging.instance.buildUI(engines: engines,                                                          configs: configs)      } catch {          print("Error creating view controller: \(error)")      }
      // Return a UINavigationController containing our UIViewController      return UINavigationController(rootViewController: viewController)  }
  //...}