Swift & Firebase: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 19: | Zeile 19: | ||
== Beispiele == | == Beispiele == | ||
| + | |||
| + | === Integration in der App === | ||
| + | <syntaxhighlight lang="swift"> | ||
| + | // | ||
| + | // AppDelegate.swift | ||
| + | // | ||
| + | |||
| + | import UIKit | ||
| + | import FirebaseCore // Import Firebase | ||
| + | |||
| + | @UIApplicationMain | ||
| + | class AppDelegate: UIResponder, UIApplicationDelegate { | ||
| + | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | ||
| + | // Override point for customization after application launch. | ||
| + | FirebaseApp.configure() | ||
| + | return true | ||
| + | } | ||
| + | // ... | ||
| + | } | ||
| + | </syntaxhighlight> | ||
=== User Authentication mit Firebase === | === User Authentication mit Firebase === | ||
| − | '''Passwort Authentication:''' | + | '''Passwort Authentication:''' Hier nur wenige Zeilen notwendig. Die User kannst du dann in deinem Firebase Backend anschauen. |
https://firebase.google.com/docs/auth/ios/password-auth | https://firebase.google.com/docs/auth/ios/password-auth | ||
| + | <syntaxhighlight lang="swift"> | ||
| + | // RegisterViewController.swift | ||
| + | import UIKit | ||
| + | import FirebaseCore | ||
| + | import FirebaseAuth | ||
| + | |||
| + | class RegisterViewController: UIViewController { | ||
| + | |||
| + | @IBOutlet weak var emailTextfield: UITextField! | ||
| + | @IBOutlet weak var passwordTextfield: UITextField! | ||
| + | |||
| + | @IBAction func registerPressed(_ sender: UIButton) { | ||
| + | if let email = emailTextfield.text, let password = passwordTextfield.text { | ||
| + | Auth.auth().createUser(withEmail: email, password: password) { authResult, error in | ||
| + | if let e = error { | ||
| + | // Handle errors here | ||
| + | print(e) | ||
| + | }else{ | ||
| + | // Navigate to ChatViewController | ||
| + | self.performSegue(withIdentifier: "RegisterToChat", sender: self) | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="swift"> | ||
| + | </syntaxhighlight> | ||
Version vom 23. Januar 2023, 17:08 Uhr
Links
Swift (Programmiersprache)
Quickstart
- Projekt in Firebase anlegen
- Firebase zur App hinzufügen
- Plattform auswählen (iOS) (die nachfolgenden Schritte werden ganz gut erklärt)
- App Registrieren
- mind. Bundle-Identifier der App hinzufügen
- Konfigurationsdatei laden und zur App hinzufügen (Stammverzeichnis wo auch Info.plist liegt)
- Firebase SDK hinzufügen (mehrere Möglichkeiten z.B. Cocoapods oder Swift Package Manager)
- SPM: Link kopieren (i.e. https://github.com/firebase/firebase-ios-sdk) und über Xcode installieren
- Cocoapods: In Podfile hinzufügen und über pod install installieren (siehe Cocoapods)
- Initialisierungscode zu AppDelegate.swift hinzufügen
- Hier importierst und initialisierst du Firebase. Den passenden Code für Swift, SwiftUI oder Apps mit ObjC hinzufügen. Schau einfach den Code der AppDelegate an, dann findest du die passenden Ergänzungen.Z.B.
import FirebaseCore // .. FirebaseApp.configure() // in didFinishLaunchingWithOptions
Beispiele
Integration in der App
//
// AppDelegate.swift
//
import UIKit
import FirebaseCore // Import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
// ...
}
User Authentication mit Firebase
Passwort Authentication: Hier nur wenige Zeilen notwendig. Die User kannst du dann in deinem Firebase Backend anschauen.
https://firebase.google.com/docs/auth/ios/password-auth
// RegisterViewController.swift
import UIKit
import FirebaseCore
import FirebaseAuth
class RegisterViewController: UIViewController {
@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!
@IBAction func registerPressed(_ sender: UIButton) {
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let e = error {
// Handle errors here
print(e)
}else{
// Navigate to ChatViewController
self.performSegue(withIdentifier: "RegisterToChat", sender: self)
}
}
}
}
}