Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple example of how to use notifications to pass data
// MARK: LoginViewController
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(handleLogin(notification:)),
name: Notification.Name("UserIdentifier"),
object: nil)
}
func login(with username: String,
password: String) {
let authenticationSession = AuthenticationSession()
authenticationSession.performLogin(with: username,
password: password)
}
@objc func handleLogin(notification: Notification) {
if let userInfo = notification.userInfo {
if let userIdentifier = userInfo["User Identifier"] as? String {
// Handle the login with userIdentifier
}
}
}
}
// MARK: AuthenticationSession
class AuthenticationSession {
func performLogin(with username: String,
password: String) {
let userIdentifier = "12345" //Service.login(username, password)
NotificationCenter.default.post(name: Notification.Name("UserIdentifier"), object: self, userInfo: ["User Identifier": userIdentifier])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment