DEV Community

Carpe Wang
Carpe Wang

Posted on

reCharge:基于 SwiftUI 的全方位健康睡眠管理 App 实现原理与技术分享

1. Project Background & Motivation

As modern life accelerates, sleep quality is becoming an increasingly vital part of overall well-being. Leveraging Apple Watch and iPhone health data, we can now detect sleep patterns more accurately and combine them with factors like natural light exposure, hydration, exercise, and AI-powered suggestions to build a closed-loop sleep optimization solution.

reCharge is a native SwiftUI app that integrates multiple Apple ecosystem technologies — including HealthKit, CoreLocation, WatchConnectivity, StoreKit, localization, and ChatGPT — to deliver a full-featured sleep management experience.

As an iOS developer, I created this project to explore how AI and system-level Apple APIs can combine to improve daily health through a seamless user experience.

💡 Note: The Apple Watch serves primarily as a data collection tool. In future iterations, reCharge will incorporate additional behavioral signals, such as screen time tracking, to personalize recommendations.


2. Core Features Overview

  • Sleep Data Sync: Automatically reads and displays sleep history from Apple Watch via HealthKit
  • AI-Powered Suggestions: Uses the OpenAI ChatGPT API and user health profile to generate personalized lifestyle and sleep recommendations
  • Sunlight Exposure Reminders: Calculates optimal sunlight windows based on location and sunrise/sunset times
  • Hydration & Activity Tracking: Helps users record water intake and daily exercise to support better sleep
  • Apple Watch Companion App: Displays current sleep status and reminders in real-time on watchOS
  • Subscription Paywall: Integrated with StoreKit 4 to support in-app purchases, trials, and subscription tiers
  • Localization Support: Seamless language switching with built-in English/Chinese support and scalable i18n architecture

3. Tech Stack & Architecture

  • UI Layer: SwiftUI + Combine using MVVM pattern
  • Data & Permissions: HealthKitManager, LocationManager, NotificationManager
  • Watch Communication: WatchConnectivityManager
  • Networking: SunTimeAPIService (RESTful) + ChatGPTService (OpenAI)
  • Payments: StoreKitManager + SubscriptionManager
  • Localization: LanguageManager with .lproj resource bundles
  • Persistence: UserDefaults for settings / Keychain for secure tokens (optional upgrade to CoreData)

4. Key Module Breakdowns

1. HealthKitManager

  • Requests permissions for sleep, heart rate, steps, etc.
  • Exposes Combine publishers for batch queries and real-time updates
healthStore.requestAuthorization(toShare: [], read: [.sleepAnalysis]) { success, error in
    // Handle authorization
}
Enter fullscreen mode Exit fullscreen mode

2. SunTimeAPIService & SunTimeManager

  • Parses sunrise/sunset times to schedule local notifications

  • LocationPermissionCard and RequestLocationButton handle runtime permission flow

3.WatchConnectivityManager

  • Manages WCSession between iPhone and Apple Watch

  • Syncs real-time sleep and reminder states

  • Updates watchOS interface via corresponding SleepManager

4. StoreKitManager & SubscriptionManager

  • Queries available products, handles purchases and restoration logic

  • PaywallView governs subscription flow

  • Subscription state is stored in UserDefaults

5. Project Highlights & Engineering Practices

Full Apple Ecosystem Integration: iPhone + Watch + HealthKit + StoreKit + ChatGPT

  • MVVM + Combine Architecture: Clean separation of UI and logic, testable reactive data flow

  • Modular Design: Well-encapsulated services and managers with low coupling

  • Internationalization Ready: Scalable .lproj file structure, easily expandable

  • AI Recommendations: Demonstrates real-world integration of LLMs into mobile health workflows

6.Future Improvements

  • Add sleep interventions (e.g. white noise, breathing exercises)

  • Visualize health data using line charts and heatmaps

  • Introduce intelligent reminder strategies based on ML or user habits

  • Expand to additional health metrics (e.g. blood oxygen, HRV)

  • Enable full offline functionality and iCloud sync/backup

7. Conclusion

reCharge is a showcase of what’s possible when combining modern SwiftUI development with the power of the Apple ecosystem and AI-driven personalization. Whether you're looking to integrate HealthKit, implement watchOS communication, or embed GPT recommendations, this project offers a complete blueprint to learn from.

Top comments (0)