NetworkManager is a simple and lightweight class written in SwiftUI to handle all of your network state management in your iOS projects. It provides a clean and easy-to-use interface for monitoring network connectivity, and updating your app's UI accordingly.
Here's the list of the awesome features NetworkManager
has:
- Simple and easy-to-use interface
- Automatically updates the UI when the network state changes
- Monitors the network state in real-time.
- Provides an observable object for binding to in SwiftUI.
- Can be easily integrated with other parts of your app to handle network-related logic.
To install NetworkStateManager, simply add the NetworkStateManager.swift file to your Xcode project and enjoy 🙂.
First create a NetworkState
object and add it as an environmentObject
to your root View
.
import SwiftUI
@main
struct NetworkStateDemoApp: App {
let networkState = NetworkState()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(networkState)
}
}
}
Use the isConnected
variable to render out your UI. For more understanding download and run the project.
import SwiftUI
struct ContentView: View {
@EnvironmentObject var networkManager: NetworkManager
var body: some View {
VStack(spacing: 20) {
Image(systemName: networkManager.isConnected ? "wifi" : "wifi.slash").font(.largeTitle)
let status = networkManager.isConnected == true ? "Online" : "Offline"
Text("You are \(status) and connected to \(networkManager.networkType?.rawValue ?? "")")
.foregroundColor(.green)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can also customize the behavior of NetworkManager by subclassing it and overridding the methods you want to change.
Your contributions and suggestions are always welcome. Feel free to open an issue or create a pull request.
This is just a sample README.md, you can add more details, examples and so on based on the actual implementation of your NetworkManager.swift class.