-
-
Save zontan/e4e4c5e3960b92501fd173f24b450d95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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