From c701ac40a6954e898683ef1c0c4ba6610e8199a9 Mon Sep 17 00:00:00 2001 From: Ross Butler Date: Sun, 15 May 2022 12:21:20 +0100 Subject: [PATCH] Implemented framework configuration where using a publisher --- CHANGELOG.md | 12 +++++++++ Connectivity.podspec | 2 +- .../Combine/ConnectivityPublisher.swift | 11 +++++--- .../Combine/ConnectivitySubscription.swift | 5 ++-- .../Connectivity.xcodeproj/project.pbxproj | 8 +++--- .../Connectivity/CombineViewController.swift | 19 ++++++++------ Example/Pods/Pods.xcodeproj/project.pbxproj | 8 +++--- README.md | 26 ++++++++++++++++--- 8 files changed, 64 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57aa859..27f7e5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [5.3.0] - 2022-05-15 +### Added +A Connectivity publisher now accepts a configuration object which can be used to configure the framework. + +```swift +let publisher = Connectivity.Publisher( + configuration: + .init() + .configureURLSession(.default) +) +``` + ## [5.2.0] - 2022-05-14 ### Added - Fluent configuration API: Connectivity may now be configured by passing a `ConnectivityConfiguration` object to the initializer. diff --git a/Connectivity.podspec b/Connectivity.podspec index 5dafe8f..42a68d6 100644 --- a/Connectivity.podspec +++ b/Connectivity.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Connectivity' - s.version = '5.2.0' + s.version = '5.3.0' s.swift_version = '5.0' s.summary = 'Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access.' s.description = <<-DESC diff --git a/Connectivity/Classes/Combine/ConnectivityPublisher.swift b/Connectivity/Classes/Combine/ConnectivityPublisher.swift index 8154bbe..f9845f7 100644 --- a/Connectivity/Classes/Combine/ConnectivityPublisher.swift +++ b/Connectivity/Classes/Combine/ConnectivityPublisher.swift @@ -14,12 +14,15 @@ import Foundation public struct ConnectivityPublisher: Publisher { public typealias Output = Connectivity public typealias Failure = Never - - public init() {} - + private let configuration: ConnectivityConfiguration + + public init(configuration: ConnectivityConfiguration = ConnectivityConfiguration()) { + self.configuration = configuration + } + public func receive(subscriber: S) where ConnectivityPublisher.Failure == S.Failure, ConnectivityPublisher.Output == S.Input { - let subscription = ConnectivitySubscription(subscriber: subscriber) + let subscription = ConnectivitySubscription(configuration: configuration, subscriber: subscriber) subscriber.receive(subscription: subscription) } } diff --git a/Connectivity/Classes/Combine/ConnectivitySubscription.swift b/Connectivity/Classes/Combine/ConnectivitySubscription.swift index af513bb..e972a8a 100644 --- a/Connectivity/Classes/Combine/ConnectivitySubscription.swift +++ b/Connectivity/Classes/Combine/ConnectivitySubscription.swift @@ -12,10 +12,11 @@ import Foundation @available(OSX 10.15, iOS 13.0, tvOS 13.0, *) class ConnectivitySubscription: Subscription where S.Input == Connectivity, S.Failure == Never { - private let connectivity = Connectivity() + private let connectivity: Connectivity private var subscriber: S? - init(subscriber: S) { + init(configuration: ConnectivityConfiguration, subscriber: S) { + connectivity = Connectivity(configuration: configuration) self.subscriber = subscriber startNotifier(with: subscriber) } diff --git a/Example/Connectivity.xcodeproj/project.pbxproj b/Example/Connectivity.xcodeproj/project.pbxproj index 737037d..e0e7c7a 100644 --- a/Example/Connectivity.xcodeproj/project.pbxproj +++ b/Example/Connectivity.xcodeproj/project.pbxproj @@ -785,7 +785,7 @@ GCC_TREAT_WARNINGS_AS_ERRORS = YES; INFOPLIST_FILE = Connectivity/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -804,7 +804,7 @@ GCC_TREAT_WARNINGS_AS_ERRORS = YES; INFOPLIST_FILE = Connectivity/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -865,7 +865,7 @@ INFOPLIST_FILE = Connectivity_Example_macOS/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example-macOS"; @@ -897,7 +897,7 @@ INFOPLIST_FILE = Connectivity_Example_macOS/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example-macOS"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/Example/Connectivity/CombineViewController.swift b/Example/Connectivity/CombineViewController.swift index 420e5e8..f97ec33 100644 --- a/Example/Connectivity/CombineViewController.swift +++ b/Example/Connectivity/CombineViewController.swift @@ -15,13 +15,13 @@ import UIKit @available(iOS 13.0, *) class CombineViewController: UIViewController { // MARK: Outlets - + @IBOutlet var activityIndicator: UIActivityIndicatorView! @IBOutlet var notifierButton: UIButton! @IBOutlet var statusLabel: UILabel! - + // MARK: Status - + fileprivate var isCheckingConnectivity: Bool = false private var cancellable: AnyCancellable? } @@ -39,9 +39,12 @@ extension CombineViewController { private extension CombineViewController { func startConnectivityChecks() { activityIndicator.startAnimating() - let publisher = Connectivity.Publisher() + let publisher = Connectivity.Publisher( + configuration: + .init() + .configureURLSession(.default) + ).eraseToAnyPublisher() cancellable = publisher.receive(on: DispatchQueue.main) - .eraseToAnyPublisher() .sink(receiveCompletion: { [weak self] _ in guard let strongSelf = self else { return @@ -55,14 +58,14 @@ private extension CombineViewController { isCheckingConnectivity = true updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity) } - + func stopConnectivityChecks() { activityIndicator.stopAnimating() cancellable?.cancel() isCheckingConnectivity = false updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity) } - + func updateConnectionStatus(_ status: Connectivity.Status) { switch status { case .connectedViaWiFi, .connectedViaCellular, .connected, .connectedViaEthernet: @@ -74,7 +77,7 @@ private extension CombineViewController { } statusLabel.text = status.description } - + func updateNotifierButton(isCheckingConnectivity: Bool) { let buttonText = isCheckingConnectivity ? "Stop notifier" : "Start notifier" let buttonTextColor = isCheckingConnectivity ? UIColor.red : UIColor.darkGreen diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index 82f773e..1d69fe4 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -1043,7 +1043,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULEMAP_FILE = "Target Support Files/Connectivity-macOS/Connectivity-macOS.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.connectivity; PRODUCT_MODULE_NAME = Connectivity; @@ -1106,7 +1106,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULEMAP_FILE = "Target Support Files/Connectivity-iOS/Connectivity-iOS.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.connectivity; PRODUCT_MODULE_NAME = Connectivity; @@ -1139,7 +1139,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULEMAP_FILE = "Target Support Files/Connectivity-iOS/Connectivity-iOS.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.connectivity; PRODUCT_MODULE_NAME = Connectivity; @@ -1392,7 +1392,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 5.2.0; + MARKETING_VERSION = 5.3.0; MODULEMAP_FILE = "Target Support Files/Connectivity-macOS/Connectivity-macOS.modulemap"; PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.connectivity; PRODUCT_MODULE_NAME = Connectivity; diff --git a/README.md b/README.md index 779f940..d6ee57d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Connectivity's objective is to solve the captive portal problem whereby an iOS d To learn more about how to use Connectivity, take a look at the [keynote presentation](https://github.com/rwbutler/Connectivity/blob/master/docs/presentations/connectivity.pdf), check out the [blog post](https://medium.com/@rwbutler/solving-the-captive-portal-problem-on-ios-9a53ba2b381e), or make use of the table of contents below: - [Features](#features) -- [What's New in Connectivity 5.2.0?](#whats-new-in-connectivity-520) +- [What's New in Connectivity 5.3.0?](#whats-new-in-connectivity-530) - [Hyperconnectivity](#hyperconnectivity) - [Installation](#installation) - [Cocoapods](#cocoapods) @@ -51,9 +51,19 @@ To learn more about how to use Connectivity, take a look at the [keynote present - [x] Polling connectivity checks may be performed where a constant network connection is required (optional). - [x] Combine support via [`Connectivity.Publisher`](https://github.com/rwbutler/Connectivity/blob/master/Connectivity/Classes/Combine/ConnectivityPublisher.swift). -## What's new in Connectivity 5.2.0? +## What's new in Connectivity 5.3.0? -Connectivity 5.2.0 provides a new fluent interface for configuring the Connectivity framework. See [Configuration](#configuration) for more information. +Connectivity 5.3.0 provides a new fluent interface for configuring the Connectivity framework. See [Configuration](#configuration) for more information. + +This allows you to configure the framework when making use of Combine publishers e.g. + +```swift +let publisher = Connectivity.Publisher( + configuration: + .init() + .configureURLSession(.default) +) +``` ## Hyperconnectivity @@ -214,7 +224,15 @@ Then remember to call `connectivity.stopNotifier()` when you are done. ### Combine Connectivity provides support for [Combine](https://developer.apple.com/documentation/combine) via the provision of [`Connectivity.Publisher`](https://github.com/rwbutler/Connectivity/blob/master/Connectivity/Classes/Combine/ConnectivityPublisher.swift) which allows a client to subscribe and be notified of changes in Internet connectivity. -The provided sample application includes [CombineViewController.swift](./Example/Connectivity/CombineViewController.swift) illustrating how to use Connectivity with the Combine framework. +The provided sample application includes [CombineViewController.swift](./Example/Connectivity/CombineViewController.swift) illustrating how to use Connectivity with the Combine framework e.g. + +```swift +let publisher = Connectivity.Publisher( + configuration: + .init() + .configureURLSession(.default) +) +``` Note: You do not need to call `startNotifier` when using `ConnectivityPublisher` - this will be done for you automatically on subscription.