Make a fully working SwiftData Notes CRUD app with Apple Sign In. Make sure the UI is a…
- User
Make a fully working SwiftData Notes CRUD app with Apple Sign In. Make sure the UI is a esthetically beautiful
- Assistant
Make a fully working SwiftData Notes CRUD app with Apple Sign In. Make sure the UI is a esthetically beautiful
In this guide, we'll walk through building a fully functional Notes app for iOS using SwiftUI, SwiftData, and integrating Sign In with Apple for user authentication. The app will support Create, Read, Update, and Delete (CRUD) operations for notes, and we'll focus on designing an aesthetically pleasing user interface to enhance the user experience.
We'll create a SignInView that allows users to authenticate using their Apple ID.
In your SignInView.swift file:
import SwiftUI
import AuthenticationServices
struct SignInView: View {
@EnvironmentObject var appState: AppState
var body: some View {
ZStack {
// Background Image
Image("background") // Add an image named "background" to your Assets
.resizable()
.scaledToFill()
.edgesIgnoringSafeArea(.all)
// Content
VStack {
Spacer()
Text("Welcome to Notes")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(.white)
.padding()
Spacer()
SignInWithAppleButton(
onRequest: { request in
request.requestedScopes = [.fullName, .email]
},
onCompletion: handleAuthorization
)
.signInWithAppleButtonStyle(.whiteOutline)
.frame(height: 50)
.padding()
}
}
}
func handleAuthorization(result: Result<ASAuthorization, Error>) {
switch result {
case .success(let authResults):
print("Authorization successful")
// Update your authentication state here
appState.isAuthenticated = true
case .failure(let error):
print("Authorization failed: \(error.localizedDescription)")
}
}
}
AuthenticationServices to access Sign In with Apple components.SignInWithAppleButton provides a standardized button for authentication.handleAuthorization processes the result of the authentication attempt.appState.isAuthenticated is set to true upon successful authentication.We'll define a Note model using SwiftData.
In your Note.swift file:
import Foundation
import SwiftData
@Model
class Note: Identifiable {
@Attribute(.unique) var id: UUID
var title: String
var content: String
var dateCreated: Date
init(id: UUID = UUID(), title: String, content: String, dateCreated: Date = Date()) {
self.id = id
self.title = title
self.content = content
self.dateCreated = dateCreated
}
}
@Model marks the class as a SwiftData model.@Attribute(.unique) ensures each note has a unique identifier.NotesView.swift
import SwiftUI
import SwiftData
struct NotesView: View {
@EnvironmentObject var appState: AppState
@Environment(\.modelContext) private var modelContext
@Query private var notes: [Note]
@State private var showAddNote = false
var body: some View {
NavigationView {
List {
ForEach(notes) { note in
NavigationLink(destination: NoteDetailView(note: note)) {
VStack(alignment: .leading, spacing: 4) {
Text(note.title)
.font(.headline)
Text(note.content)
.font(.subheadline)
.lineLimit(1)
.foregroundColor(.secondary)
Text(note.dateCreated, style: .date)
.font(.caption)
.foregroundColor(.gray)
}
.padding(.vertical, 8)
}
}
.onDelete(perform: deleteNote)
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("My Notes")
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: signOut) {
Text("Sign Out")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: { showAddNote = true }) {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $showAddNote) {
AddNoteView()
}
}
}
private func deleteNote(at offsets: IndexSet) {
for index in offsets {
let note = notes[index]
modelContext.delete(note)
}
}
private func signOut() {
appState.isAuthenticated = false
}
}
AddNoteView.swift
import SwiftUI
import SwiftData
struct AddNoteView: View {
@Environment(\.dismiss) var dismiss
@Environment(\.modelContext) private var modelContext
@State private var title = ""
@State private var content = ""
var body: some View {
NavigationView {
VStack {
Form {
Section(header: Text("Title")) {
TextField("Enter title", text: $title)
}
Section(header: Text("Content")) {
TextEditor(text: $content)
.frame(height: 200)
}
}
.navigationTitle("New Note")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
let newNote = Note(title: title, content: content)
modelContext.insert(newNote)
dismiss()
}
.disabled(title.isEmpty || content.isEmpty)
}
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
}
}
}
}
}
NoteDetailView.swift
import SwiftUI
import SwiftData
struct NoteDetailView: View {
@Environment(\.modelContext) private var modelContext
@State var note: Note
@State private var isEditing = false
var body: some View {
VStack(alignment: .leading, spacing: 16) {
if isEditing {
TextField("Title", text: $note.title)
.font(.title)
.padding(.horizontal)
TextEditor(text: $note.content)
.padding()
.frame(height: 300)
} else {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text(note.title)
.font(.largeTitle)
.fontWeight(.bold)
Text("Created on \(note.dateCreated, style: .date)")
.font(.caption)
.foregroundColor(.gray)
Divider()
Text(note.content)
.font(.body)
}
.padding()
}
}
}
.navigationTitle("Note Details")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(isEditing ? "Done" : "Edit") {
if isEditing {
// Changes are automatically saved in SwiftData
}
isEditing.toggle()
}
}
}
}
}
@Query and modelContext.// Inside SignInView
Text("Welcome to Notes")
.font(.system(size: 34, weight: .bold))
.foregroundColor(.white)
InsetGroupedListStyle for a modern look.// Inside NotesView
.listStyle(InsetGroupedListStyle())
.accentColor(.purple)
ScrollView for content that may exceed the screen's height..sheet(isPresented: $showAddNote) {
AddNoteView()
.transition(.move(edge: .bottom))
.animation(.easeInOut)
}
We'll manage the user's authentication state to toggle between the SignInView and the main NotesView.
AppState.swift
import Combine
class AppState: ObservableObject {
@Published var isAuthenticated: Bool = false
}
SwiftDataNotesApp.swift
import SwiftUI
import SwiftData
@main
struct SwiftDataNotesApp: App {
@StateObject private var appState = AppState()
var body: some Scene {
WindowGroup {
if appState.isAuthenticated {
NotesView()
.environmentObject(appState)
} else {
SignInView()
.environmentObject(appState)
}
}
.modelContainer(for: Note.self)
}
}
ObservableObject that tracks authentication status.SwiftDataNotesApp uses @StateObject to keep appState alive.appState using @EnvironmentObject.You've successfully built a fully functional Notes app with SwiftUI, SwiftData, and Apple Sign In. We've emphasized both functionality and aesthetics to create an app that's not only useful but also visually appealing.
Feel free to ask if you have any questions or need further assistance!
Not yet included in a published release.