A collection of reusable Swift components for your iOS projects.
- π¦ Features
- π² Installation
- π Usage
- π₯ Why Choose SwiftCoreUtilities?
- π€ Contributing
- βοΈ License
- π Modular Architecture β Service Locator for easy dependency injection
- π Observer Pattern Implementation β Custom event-driven notification system for seamless communication between components
- πΎ Data Persistence β Easy to use abstraction for CoreData, SwiftData, Keychain and UserDefaults
- π Networking β Simple API client with request builder and error handling
- π User Location Tracking β Efficient CoreLocation integration with heading & accelerometer support
- π Permissions Management β Centralized access for Location, Notifications, Camera, Photos, and Bluetooth
- π‘ Background Task Management β Periodic background execution with iOS BGTaskScheduler
- π¨ UI Utilities β SwiftUI modifiers, helpers for navigation, keyboard, animations, etc..
You can install SwiftCoreUtilities via SPM:
- Open Xcode, go to Project Navigator and select your project.
- Click on Package Dependencies in the project settings.
- Click the + button at the bottom left to add a new package.
- Enter the repo URL:
https://github.com/jordantete/SwiftCoreUtilities.git
- Choose the Latest Version or a specific version you need, then click Add Package.
- Select SwiftCoreUtilities as a dependency for your target and click Add Package.
Now you can import and start using the utilities in your project:
import SwiftCoreUtilities
import SwiftCoreUtilities
class MyObserver: Observer {
func update(event: NotificationEvent) {
switch event {
case .permissionsChanged(let permissionType, let state):
print("Permission changed: \(permissionType) - New state: \(state)")
case .syncStateChanged(let state):
print("Sync state changed: \(state)")
}
}
}
let observer = MyObserver()
NotificationManager.shared.addObserver(observer)
To emit an event from anywhere in your app:
NotificationManager.shared.post(event: .aNotificationEvent(myEventValue: "aString"))
SwiftCoreUtilities offers multiple storage solutions, allowing developers to choose the best persistence layer for their needs.
CoreData is a powerful framework for managing structured data. The CoreDataManager simplifies entity creation, fetching, and deletion.
import SwiftCoreUtilities
let coreDataManager = CoreDataManagerImpl()
// Saving a new entity
try? coreDataManager.save(StoredEntity.self) { entity in
entity.name = "Example Data"
entity.timestamp = Date()
}
// Fetching entities
let storedEntities: [StoredEntity] = try coreDataManager.fetchData(entity: StoredEntity.self)
// Deleting an entity
if let entity = storedEntities.first {
coreDataManager.delete(entity)
}
// Deleting all entities
coreDataManager.deleteAll(entity: StoredEntity.self)
For apps targeting iOS 17+, SwiftData provides a simpler and more Swift-native approach to persistence.
import SwiftCoreUtilities
import SwiftData
@Model
class UserProfile {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
// Creating a model and storing data
let userProfile = UserProfile(name: "Alice", age: 28)
SwiftDataManager.shared.save(userProfile)
// Fetching all stored profiles
let profiles: [UserProfile] = SwiftDataManager.shared.fetch(UserProfile.self)
// Deleting a specific object
if let profile = profiles.first {
SwiftDataManager.shared.delete(profile)
}
// Deleting all profiles
SwiftDataManager.shared.deleteAll(UserProfile.self)
π SwiftData is recommended for simpler models and modern apps targeting iOS 17+. π CoreData remains the best choice for complex data relationships and compatibility with iOS 16+.
For securely storing API keys, authentication tokens, and user credentials, KeychainManager provides an easy-to-use interface.
import SwiftCoreUtilities
let keychain = KeychainManager()
// Storing sensitive data
try? keychain.save("my_secure_token", forKey: "AuthToken")
// Retrieving stored data
if let token: String = try? keychain.get(forKey: "AuthToken") {
print("Retrieved token:", token)
}
// Deleting sensitive data
try? keychain.delete(forKey: "AuthToken")
π Use Keychain for credentials, API keys, and any sensitive user data. π It ensures data remains secure, even after the app is deleted and reinstalled.
import SwiftCoreUtilities
let networkService = NetworkServiceImpl()
let request = APIRequest(url: "https://api.example.com/data", method: .get)
Task {
do {
let response: MyDecodableModel = try await networkService.request(request, expecting: MyDecodableModel.self)
print("Success:", response)
} catch {
print("Request failed:", error)
}
}
import SwiftCoreUtilities
class MyLocationDelegate: UserLocationManagerDelegate {
func didUpdateLocation(_ location: CLLocation) {
print("New location:", location)
}
func didFailWithError(_ error: Error) {
print("Location error:", error)
}
}
let locationManager = UserLocationManagerImpl()
locationManager.delegate = MyLocationDelegate()
locationManager.startTracking()
import SwiftCoreUtilities
let permissionService = PermissionServiceImpl(
locationPermissionManager: LocationPermissionManagerImpl(),
notificationPermissionManager: NotificationPermissionManagerImpl()
)
permissionService.requestPermission(for: .location) { state in
print("Location permission state:", state)
}
let notificationStatus = permissionService.permissionState(for: .notification)
print("Current notification permission:", notificationStatus)
import SwiftCoreUtilities
let backgroundSyncService = BackgroundSyncServiceImpl()
backgroundSyncService.startSyncing {
print("Executing periodic background task")
}
For a full demonstration of available UI utilities, see the Test App included in the repository.
- Modular & Lightweight β Pick and use only the components you need.
- Swift Concurrency β Leverages modern async/await patterns.
- Cross-Project Compatibility β Works with any iOS app, including UIKit & SwiftUI.
- Well-Tested & Maintainable β Unit tests included to ensure stability.
We welcome contributions! Feel free to fork, submit a pull request, or open an issue if you have suggestions.
This project is licensed under the MIT License β see the LICENSE file for details.