Iron Habits: Native iOS Habit Tracker with SwiftUI, CoreData & OpenAI
SwiftUI · CoreData · HealthKit · OpenAI GPT · Push Notifications · Gamification
The Problem
Most habit tracking apps suffer from the same flaw: they ask users to manually log every completion, multiple times a day. Compliance drops sharply after the first week. The engagement curve is steep and most users abandon the app within 14 days.
Iron Habits was designed to reverse this pattern — reducing manual friction to near zero through HealthKit auto-completion, intelligent notification timing, and an AI layer that helps users discover the right habits for their goals rather than building arbitrary streaks.
Technical Implementation
SwiftUI Architecture
The app is built entirely in SwiftUI with a clean MVVM architecture. ViewModels conform to ObservableObject and are injected via the environment. The UI layer is intentionally thin — all business logic lives in dedicated service classes that are testable independently of the view layer.
CoreData Architecture
Habits, completions, streaks, and user stats are persisted in CoreData with a carefully designed schema to support efficient streak calculation queries. A background context handles batch insertions (e.g. bulk HealthKit imports) without blocking the main thread. NSFetchedResultsController drives the habit list with automatic incremental diff updates.
HealthKit Auto-Completion
Habits linked to HealthKit data types (steps, workouts, sleep, mindful minutes, etc.) are automatically marked complete when the corresponding HealthKit sample is recorded — eliminating manual logging entirely for supported activity types. Background delivery via HKObserverQuery ensures completions are recorded even when the app is backgrounded.
Intelligent Notification Scheduling
Reminders are not fixed-time — they adapt to the user's completion history. The scheduling algorithm analyses the historical distribution of when a user typically completes each habit and shifts future reminder times to just before the user's typical completion window. This significantly improves on-time completion rates compared to static reminders.
Gamification Engine
A custom gamification engine awards XP, unlocks badges, and maintains streak multipliers. Streaks are calculated at the day boundary using a background task that runs at midnight — handling timezone changes and DST transitions correctly, a common edge case in habit apps. Level progression curves are designed to reward early engagement without making long-term progress feel unattainable.
OpenAI GPT Habit Recommendations
Users can describe a goal in natural language ("I want to sleep better", "I want to run a 5K in 3 months") and receive a personalised habit plan generated by GPT-4. The prompt includes the user's current habit load, historical completion rate, and available time windows — ensuring recommendations are realistic and achievable rather than aspirational and abandoned.
Technical Stack
Key Engineering Challenges Solved
- Correct streak calculation at day boundaries across timezones and DST transitions
- Background HealthKit delivery ensuring completions register even when the app is not open
- Efficient CoreData queries for streak calculation over potentially thousands of completion records
- Adaptive notification scheduling without over-prompting or notification fatigue
- GPT prompt engineering to produce realistic, personalised habit plans rather than generic advice
Need a Native iOS Developer?
I build production-quality iOS apps using Swift and SwiftUI. Tell me what you're working on.
Say hi