Skip to content

Repository files navigation

NetworkConnectivityKit

Swift VersionPlatformsSwift Package ManagerLicense

NetworkConnectivityKit is a modern, lightweight Swift library for testing real network connectivity on Apple platforms. Unlike simple reachability checks, this library performs actual HTTP requests to well-known endpoints to determine if internet connectivity is truly available.

Why NetworkConnectivityKit?

Traditional reachability APIs only check if a network interface is available, but they can't detect:

  • Captive portals (like hotel or airport WiFi login pages)
  • Restricted networks with limited internet access
  • Network configurations that block specific traffic

NetworkConnectivityKit solves these problems by making real HTTP requests to reliable endpoints and validating the responses, giving you confidence that your app can actually reach the internet.

Need monitor system network reachability and information? Check out NetworkPathMonitor - A modern, type-safe, network path monitoring utility for Apple platforms using Swift Concurrency.

Requirements

  • Swift 5.10+
  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ / visionOS 1.0+

Installation

Swift Package Manager

Add NetworkConnectivityKit to your project using Xcode or by adding it to your Package.swift:

dependencies:[.package(url:"https://github.com/iranqiu/NetworkConnectivityKit.git", from:"1.1.0")]

Then import the module:

import NetworkConnectivityKit

Quick Start

Basic Connectivity Check

The simplest way to check connectivity:

import NetworkConnectivityKit
// Check connectivity using default endpoints
letisConnected=awaitNetworkConnectivityKit.checkConnectivity()if isConnected {print("Internet is available")}else{print("No internet connectivity")}

Single Endpoint Testing

Test connectivity against a specific endpoint:

// Test using Apple's captive portal endpoint
letisConnected=awaitNetworkConnectivityKit.checkConnectivity(using:.appleCaptive)
// Test using Google's generate_204 endpoint
letisConnected=awaitNetworkConnectivityKit.checkConnectivity(using:.googleGstatic)

Advanced Usage

Testing Multiple Endpoints

Test multiple endpoints concurrently for better reliability:

// Use predefined endpoint sets
letisConnected=awaitNetworkConnectivityKit.checkConnectivity(using:.allDefault)
// Or create a custom set (no optional handling needed!)
letmethods:Set<NetworkConnectivityKit.ConnectivityMethod>=[.appleCaptive,.googleGstatic,.cloudflare
]letisConnected=awaitNetworkConnectivityKit.checkConnectivity(using: methods)

Custom Endpoints

Create your own connectivity test endpoints with compile-time or runtime URL validation:

// Compile-time safe StaticString URLs (recommended for hardcoded URLs)
letstaticMethod=NetworkConnectivityKit.ConnectivityMethod(
staticURLString:"http://www.gstatic.com/generate_204",
validation:.generate204Validation
)
// Runtime URL validation (for dynamic URLs)
letdynamicMethod=NetworkConnectivityKit.ConnectivityMethod(
urlString:"https://api.example.com/health",
validation:.generate200Validation
)
// Complex custom validation
letadvancedValidation=NetworkConnectivityKit.ConnectivityValidation{ request, response, data in
response.statusCode ==200 && data.count >10}letadvancedMethod=NetworkConnectivityKit.ConnectivityMethod(
staticURLString:"https://api.example.com/ping",
validation: advancedValidation
)
// All methods can be used directly
letresult1=awaitNetworkConnectivityKit.checkConnectivity(using: staticMethod)letresult2=awaitNetworkConnectivityKit.checkConnectivity(using: dynamicMethod!)letresult3=awaitNetworkConnectivityKit.checkConnectivity(using: advancedMethod)

Configuration Options

Customize timeout, caching, and other network settings:

// Create custom configuration
letconfig=NetworkConnectivityKit.ConnectivityConfiguration(
timeout:5.0, // 5 second timeout
cachePolicy:.ignoreCache, // Always make fresh requests
allowsCellularAccess:true // Allow cellular connections
)
// Create method with custom configuration (StaticString version)
letmethod=NetworkConnectivityKit.ConnectivityMethod(
staticURLString:"https://api.example.com/check",
validation:.generate200Validation,
configuration: config
)
// Or use fluent configuration
letfluentConfig=NetworkConnectivityKit.ConnectivityConfiguration.default
.timeout(10.0).ignoreCache()letisConnected=awaitNetworkConnectivityKit.checkConnectivity(using: method)

Built-in Endpoints

NetworkConnectivityKit includes several well-tested endpoints (all non-optional and compile-time safe):

EndpointURLExpected ResponseDescription
.appleCaptivehttp://captive.apple.comHTTP 200Apple's captive portal detection
.appleLibraryhttp://www.apple.com/library/test/success.htmlHTTP 200Apple's connectivity test page
.googleGstatichttp://www.gstatic.com/generate_204HTTP 204Google's lightweight endpoint
.cloudflarehttp://cp.cloudflare.com/generate_204HTTP 204Cloudflare's connectivity check
.microsofthttp://www.msftconnecttest.com/connecttest.txtHTTP 200Microsoft's connectivity test
.vivoWifihttp://wifi.vivo.com.cn/generate_204HTTP 204Vivo WiFi connectivity check
.miuiConnecthttp://connect.rom.miui.com/generate_204HTTP 204MIUI connectivity check

Performance

NetworkConnectivityKit is designed for minimal performance impact:

  • Lightweight requests: Uses minimal HTTP requests (usually < 1KB)
  • Concurrent execution: Multiple endpoints tested simultaneously
  • Quick timeouts: Default 3-second timeout for fast results
  • Early termination: Stops as soon as connectivity is confirmed
  • No persistent connections: Each test is independent
  • Compile-time optimization: StaticString URLs eliminate runtime URL parsing overhead

License

NetworkConnectivityKit is available under the MIT license. See the LICENSE file for more info.

Support

About

NetworkConnectivityKit is a lightweight Swift library for testing network connectivity using multiple validation methods.

Resources

Stars

20 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages