版本记录
版本号 | 时间 |
---|---|
V1.0 | 2019.02.01 星期五 |
前言
Firebase是一家实时后端数据库创业公司,它能帮助开发者很快的写出Web端和移动端的应用。自2014年10月Google收购Firebase以来,用户可以在更方便地使用Firebase的同时,结合Google的云服务。Firebase能让你的App从零到一。也就是说它可以帮助手机以及网页应用的开发者轻松构建App。通过Firebase背后负载的框架就可以简单地开发一个App,无需服务器以及基础设施。接下来几篇我们就一起看一下基于Firebase平台的开发。感兴趣的看下面几篇文章。
1. 基于Firebase平台开发(一) —— 基于ML Kit的iOS图片中文字的识别(一)
2. 基于Firebase平台开发(二) —— 基于ML Kit的iOS图片中文字的识别(二)
3. 基于Firebase平台开发(三) —— Firebase基本使用简介(一)
4. 基于Firebase平台开发(四) —— Firebase基本使用简介(二)
源码
1. Swift
首先看下工程结构
接着看下sb中的内容
下面就是一起看代码
1. GroceryItem.swift
import Foundation
import Firebase
struct GroceryItem {
let ref: DatabaseReference?
let key: String
let name: String
let addedByUser: String
var completed: Bool
init(name: String, addedByUser: String, completed: Bool, key: String = "") {
self.ref = nil
self.key = key
self.name = name
self.addedByUser = addedByUser
self.completed = completed
}
init?(snapshot: DataSnapshot) {
guard
let value = snapshot.value as? [String: AnyObject],
let name = value["name"] as? String,
let addedByUser = value["addedByUser"] as? String,
let completed = value["completed"] as? Bool else {
return nil
}
self.ref = snapshot.ref
self.key = snapshot.key
self.name = name
self.addedByUser = addedByUser
self.completed = completed
}
func toAnyObject() -> Any {
return [
"name": name,
"addedByUser": addedByUser,
"completed": completed
]
}
}
2. User.swift
import Foundation
import Firebase
struct User {
let uid: String
let email: String
init(authData: Firebase.User) {
uid = authData.uid
email = authData.email!
}
init(uid: String, email: String) {
self.uid = uid
self.email = email
}
}
3. GroceryListTableViewController.swift
import UIKit
import Firebase
class GroceryListTableViewController: UITableViewController {
// MARK: Constants
let listToUsers = "ListToUsers"
// MARK: Properties
var items: [GroceryItem] = []
var user: User!
var userCountBarButtonItem: UIBarButtonItem!
let ref = Database.database().reference(withPath: "grocery-items")
let usersRef = Database.database().reference(withPath: "online")
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: UIViewController Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsMultipleSelectionDuringEditing = false
userCountBarButtonItem = UIBarButtonItem(title: "1",
style: .plain,
target: self,
action: #selector(userCountButtonDidTouch))
userCountBarButtonItem.tintColor = UIColor.white
navigationItem.leftBarButtonItem = userCountBarButtonItem
ref.queryOrdered(byChild: "completed").observe(.value, with: { snapshot in
var newItems: [GroceryItem] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot,
let groceryItem = GroceryItem(snapshot: snapshot) {
newItems.append(groceryItem)
}
}
self.items = newItems
self.tableView.reloadData()
})
Auth.auth().addStateDidChangeListener { auth, user in
guard let user = user else { return }
self.user = User(authData: user)
let currentUserRef = self.usersRef.child(self.user.uid)
currentUserRef.setValue(self.user.email)
currentUserRef.onDisconnectRemoveValue()
}
usersRef.observe(.value, with: { snapshot in
if snapshot.exists() {
self.userCountBarButtonItem?.title = snapshot.childrenCount.description
} else {
self.userCountBarButtonItem?.title = "0"
}
})
}
// MARK: UITableView Delegate methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
let groceryItem = items[indexPath.row]
cell.textLabel?.text = groceryItem.name
cell.detailTextLabel?.text = groceryItem.addedByUser
toggleCellCheckbox(cell, isCompleted: groceryItem.completed)
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let groceryItem = items[indexPath.row]
groceryItem.ref?.removeValue()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else { return }
let groceryItem = items[indexPath.row]
let toggledCompletion = !groceryItem.completed
toggleCellCheckbox(cell, isCompleted: toggledCompletion)
groceryItem.ref?.updateChildValues([
"completed": toggledCompletion
])
}
func toggleCellCheckbox(_ cell: UITableViewCell, isCompleted: Bool) {
if !isCompleted {
cell.accessoryType = .none
cell.textLabel?.textColor = .black
cell.detailTextLabel?.textColor = .black
} else {
cell.accessoryType = .checkmark
cell.textLabel?.textColor = .gray
cell.detailTextLabel?.textColor = .gray
}
}
// MARK: Add Item
@IBAction func addButtonDidTouch(_ sender: AnyObject) {
let alert = UIAlertController(title: "Grocery Item",
message: "Add an Item",
preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { _ in
guard let textField = alert.textFields?.first,
let text = textField.text else { return }
let groceryItem = GroceryItem(name: text,
addedByUser: self.user.email,
completed: false)
let groceryItemRef = self.ref.child(text.lowercased())
groceryItemRef.setValue(groceryItem.toAnyObject())
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
@objc func userCountButtonDidTouch() {
performSegue(withIdentifier: listToUsers, sender: nil)
}
}
4. LoginViewController.swift
import UIKit
import Firebase
class LoginViewController: UIViewController {
// MARK: Constants
let loginToList = "LoginToList"
// MARK: Outlets
@IBOutlet weak var textFieldLoginEmail: UITextField!
@IBOutlet weak var textFieldLoginPassword: UITextField!
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: UIViewController Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
Auth.auth().addStateDidChangeListener() { auth, user in
if user != nil {
self.performSegue(withIdentifier: self.loginToList, sender: nil)
self.textFieldLoginEmail.text = nil
self.textFieldLoginPassword.text = nil
}
}
}
// MARK: Actions
@IBAction func loginDidTouch(_ sender: AnyObject) {
guard
let email = textFieldLoginEmail.text,
let password = textFieldLoginPassword.text,
email.count > 0,
password.count > 0
else {
return
}
Auth.auth().signIn(withEmail: email, password: password) { user, error in
if let error = error, user == nil {
let alert = UIAlertController(title: "Sign In Failed",
message: error.localizedDescription,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true, completion: nil)
}
}
}
@IBAction func signUpDidTouch(_ sender: AnyObject) {
let alert = UIAlertController(title: "Register",
message: "Register",
preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { _ in
let emailField = alert.textFields![0]
let passwordField = alert.textFields![1]
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!) { user, error in
if error == nil {
Auth.auth().signIn(withEmail: self.textFieldLoginEmail.text!,
password: self.textFieldLoginPassword.text!)
}
}
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
alert.addTextField { textEmail in
textEmail.placeholder = "Enter your email"
}
alert.addTextField { textPassword in
textPassword.isSecureTextEntry = true
textPassword.placeholder = "Enter your password"
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
}
extension LoginViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == textFieldLoginEmail {
textFieldLoginPassword.becomeFirstResponder()
}
if textField == textFieldLoginPassword {
textField.resignFirstResponder()
}
return true
}
}
5. OnlineUsersTableViewController.swift
import UIKit
import Firebase
class OnlineUsersTableViewController: UITableViewController {
// MARK: Constants
let userCell = "UserCell"
// MARK: Properties
var currentUsers: [String] = []
let usersRef = Database.database().reference(withPath: "online")
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: UIViewController Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
usersRef.observe(.childAdded, with: { snap in
guard let email = snap.value as? String else { return }
self.currentUsers.append(email)
let row = self.currentUsers.count - 1
let indexPath = IndexPath(row: row, section: 0)
self.tableView.insertRows(at: [indexPath], with: .top)
})
usersRef.observe(.childRemoved, with: { snap in
guard let emailToFind = snap.value as? String else { return }
for (index, email) in self.currentUsers.enumerated() {
if email == emailToFind {
let indexPath = IndexPath(row: index, section: 0)
self.currentUsers.remove(at: index)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
}
})
}
// MARK: UITableView Delegate methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentUsers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: userCell, for: indexPath)
let onlineUserEmail = currentUsers[indexPath.row]
cell.textLabel?.text = onlineUserEmail
return cell
}
// MARK: Actions
@IBAction func signoutButtonPressed(_ sender: AnyObject) {
let user = Auth.auth().currentUser!
let onlineRef = Database.database().reference(withPath: "online/\(user.uid)")
onlineRef.removeValue { (error, _) in
if let error = error {
print("Removing online failed: \(error)")
return
}
do {
try Auth.auth().signOut()
self.dismiss(animated: true, completion: nil)
} catch (let error) {
print("Auth sign out failed: \(error)")
}
}
}
}
6. AppDelegate.swift
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
return true
}
}
这是年前最后一篇了,马上就要去坐火车回家了,回家就没条件写了,祝大家新年快乐,年后见!!
后记
本篇主要讲述了Firebase基本使用简介,感兴趣的给个赞或者关注~~~