Skip to content

Instantly share code, notes, and snippets.

@zontan
Last active July 9, 2020 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zontan/e4e4c5e3960b92501fd173f24b450d95 to your computer and use it in GitHub Desktop.
Save zontan/e4e4c5e3960b92501fd173f24b450d95 to your computer and use it in GitHub Desktop.
class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var textField: UITextField!
weak var agoraRtm: AgoraRtmKit?
var channelName: String?
var channel: AgoraRtmChannel?
var handle: AuthStateDidChangeListenerHandle?
var currentUser: User?
var messageList: [String] = []
override func viewWillAppear(_ animated: Bool) {
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
self.currentUser = user
if user != nil, let channelName = self.channelName {
self.channel = self.agoraRtm?.createChannel(withId: channelName, delegate: self)
self.channel?.join(completion: { (error) in
if error != .channelErrorOk {
print("Error joining channel: ", error.rawValue)
}
})
} else {
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
}
}
override func viewWillDisappear(_ animated: Bool) {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
if let channel = self.channel {
channel.leave(completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment