:::iOS , register , Login
// AppDelegate.swift
import UIKit
import Firebase
@UIApplicationMain ^เป็น annotation บอกว่า class นี้เป็น entry point
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
// ContentView.swift
import SwiftUI
struct ContentView: View {
@AppStorage(“uid”) var userID: String = “”
var body: some View {
if userID == "" {
AuthView()
} else {
MainView()
}
}
}
// AuthView.swift
import SwiftUI
struct AuthView: View {
@State private var isLogin = true
var body: some View {
VStack {
if isLogin {
LoginView()
} else {
RegisterView()
}
Button(action: { isLogin.toggle() }) {
Text(isLogin ? "No account? Register" : "Have an account? Login")
}
}.padding()
}
}
// RegisterView.swift
import SwiftUI
import FirebaseAuth
struct RegisterView: View {
@State private var email = “”
@State private var password = “”
@AppStorage(“uid”) var userID: String = “”
var body: some View {
VStack {
TextField("Email", text: $email).textFieldStyle(RoundedBorderTextFieldStyle())
SecureField("Password", text: $password).textFieldStyle(RoundedBorderTextFieldStyle())
Button("Register") {
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if let result = result {
userID = result.user.uid
}
}
}
}
}
}
// LoginView.swift
import SwiftUI
import FirebaseAuth
struct LoginView: View {
@State private var email = “”
@State private var password = “”
@AppStorage(“uid”) var userID: String = “”
var body: some View {
VStack {
TextField("Email", text: $email).textFieldStyle(RoundedBorderTextFieldStyle())
SecureField("Password", text: $password).textFieldStyle(RoundedBorderTextFieldStyle())
Button("Login") {
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if let result = result {
userID = result.user.uid
}
}
}
}
}
}
// MainView.swift
import SwiftUI
import FirebaseAuth
struct MainView: View {
@AppStorage(“uid”) var userID: String = “”
var body: some View {
VStack {
Text("Welcome to the Medical App")
Button("Logout") {
try? Auth.auth().signOut()
userID = ""
}
}
}
}
@UIApplicationMain
@UIApplicationMainคือ annotation ที่ใช้ระบุว่า class นี้เป็น entry point ของแอป iOS โดยระบบจะสร้างไฟล์main.swiftให้อัตโนมัติ และเรียกAppDelegateโดยตรง
หากต้องการเปลี่ยนไปใช้ @main กับ SceneDelegate แบบใหม่ (iOS 14+), ก็สามารถปรับได้เช่นกันครับ.
Annotation
Annotation (แอนโนเทชัน) คือ “คำสั่งหรือสัญลักษณ์พิเศษ” ที่เราเขียนไว้ใน โค้ด เพื่อให้ คอมไพเลอร์ หรือ framework รู้ว่าจะต้องจัดการกับคลาสหรือฟังก์ชันนั้นอย่างไรเป็นพิเศษ
🔧 ตัวอย่างความหมายของ Annotation
ในภาษา Swift:
@UIApplicationMain
→ แจ้งระบบว่า class นี้คือ entry point ของแอป (คือmain.swift)
→ ระบบจะสร้างฟังก์ชันmain()ให้อัตโนมัติ
ในภาษา Java (หรือ Kotlin):
@Override
→ บอกว่ากำลัง override เมธอดจาก superclass@Entity
→ ใน Spring Framework บอกว่าคลาสนี้คือ table ในฐานข้อมูล
🧠 เปรียบเทียบแบบง่าย
เหมือนติด “สติ๊กเกอร์คำสั่งพิเศษ” ไว้บน class/function เพื่อให้ระบบรู้ว่าต้อง ปฏิบัติต่อมันอย่างพิเศษ
🔤 Syntax Swift ที่คล้าย Annotation
| Syntax | ความหมาย |
|---|---|
@UIApplicationMain | ประกาศ entry-point ของแอป |
@State, @Binding | ใช้ควบคุมค่าภายใน SwiftUI |
@main | ตั้งจุดเริ่มโปรแกรม (แทน UIApplicationMain) |
@objc | ใช้สำหรับ interoperability กับ Objective-C |
@available | ระบุเงื่อนไขเวอร์ชัน iOS ที่ใช้ได้ |
ถ้าคุณต้องการรู้ว่า annotation มีอะไรในภาษาอื่น เช่น Java, Python, หรือ Kotlin ก็ถามเพิ่มเติมได้เลยครับ.
swiftUI vs UIKit
การเลือกใช้ SwiftUI หรือ UIKit ในการพัฒนา iOS app มีผลต่อทั้ง โครงสร้างโปรเจกต์, การเขียนโค้ด, และ ประสิทธิภาพในการพัฒนา โดยเฉพาะอย่างยิ่งถ้าคุณทำ แอปใหม่สำหรับ iOS 13+ เช่นแอปทางการแพทย์
✅ สรุปความแตกต่าง SwiftUI vs UIKit
| หัวข้อ | SwiftUI | UIKit |
|---|---|---|
| เริ่มใช้ครั้งแรก | iOS 13 (2019) | iOS 2 (2008) |
| แนวคิดหลัก | Declarative (โค้ดคือ UI) | Imperative (สั่งทีละขั้นตอน) |
| Syntax | Text("Hello"), VStack | UILabel, UIView.addSubview() |
| โค้ดสั้น | ✔ มาก | ❌ โค้ดยาวขึ้น |
| รองรับ dark mode | ✔ โดยอัตโนมัติ | ❌ ต้องจัดการเอง |
| การทำ animation | ✔ ง่ายและสวย | ❌ ต้องใช้หลายบรรทัด |
| สร้าง view | ใช้ struct เช่น View | ใช้ class เช่น UIViewController |
| รองรับ iOS เวอร์ชันต่ำ | ❌ iOS 13+ เท่านั้น | ✔ รองรับ iOS 9 ขึ้นไป |
| เรียนรู้สำหรับมือใหม่ | ✔ เรียนง่ายกว่า | ❌ ซับซ้อนกว่า |
| การทำงานร่วมกับ Objective-C | ❌ ยากกว่า | ✔ ทำงานได้ดี |
| Tool ที่ใช้ | SwiftUI Preview, Canvas | Interface Builder, Storyboard |
| ความเสถียร (ณ ปี 2025) | ดีขึ้นมาก | เสถียรและรองรับทุกกรณี |
📌 สรุปว่าใช้ตัวไหนดี?
👉 SwiftUI เหมาะกับ
- เริ่มโปรเจกต์ใหม่ (iOS 14+)
- ทีมขนาดเล็กที่ต้องการพัฒนาเร็ว
- แพทย์/นักพัฒนาเดี่ยวที่เน้น logic และ UI เร็ว
- เน้น Maintainability (บำรุงรักษาง่าย)
👉 UIKit เหมาะกับ
- ต้องรองรับ iOS เก่า (iOS 12 หรือต่ำกว่า)
- แอปซับซ้อน เช่นต้องจัดการ gesture, drawing, openGL
- ใช้ร่วมกับ Objective-C หรือ Legacy code
💬 ตัวอย่างโค้ด
SwiftUI
var body: some View {
VStack {
Text("Welcome")
Button("Go") { print("Tapped") }
}
}
UIKit
let label = UILabel()
label.text = "Welcome"
let button = UIButton()
button.setTitle("Go", for: .normal)
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
หากคุณทำแอปทางการแพทย์ยุคใหม่ เช่น patient register, login, view records → SwiftUI เป็นตัวเลือกที่ดีมาก โดยเฉพาะถ้าคุณเน้น iOS 14 ขึ้นไป และต้องการทำ UI เร็วๆ
ต้องการตัวอย่างเฉพาะทาง เช่น SwiftUI + PDF viewer, drawing, barcode scan, ฯลฯ ผมสามารถเขียนให้ได้เช่นกันครับ.
