Learning Mobile Development

Simple iOS Chat App in 50 Lines of Code

To complete my collection of Simple Chat across iOS, Android, and Web through WebSocket

Photo by Priscilla Du Preez on Unsplash

I have previously done the Web chat with Node.js, and also the Android Simple Chat App. To feel complete, I also created a simple iOS Web Chat here.

You can get the code here.

How it is done in iOS

1. Create an ObservableObject type class

First, let’s create an ObservableObject type class, as we want to contain the messages that will get updated in the SwiftUI automatically when the messages are updated.

class WebSocketManager: ObservableObject {
@Published var messages: [String] = []
// other code}

2. Create the WebSocket object

Next, lets’ create the WebSocket Object. Unlike Android, we don’t need an external library for it. One can instantiate it directly.

--

--