Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable iOS 12 cellular connectivity monitor by default #415

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Sources/SwiftGRPC/Core/ClientNetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import SystemConfiguration
/// Original issue: https://github.com/grpc/grpc-swift/issues/337
open class ClientNetworkMonitor {
private let queue: DispatchQueue
private let useNewCellMonitor: Bool
private let callback: (State) -> Void
private let reachability: SCNetworkReachability

Expand Down Expand Up @@ -61,16 +62,21 @@ open class ClientNetworkMonitor {

/// Designated initializer for the network monitor. Initializer fails if reachability is unavailable.
///
/// - Parameter host: Host to use for monitoring reachability.
/// - Parameter queue: Queue on which to process and update network changes. Will create one if `nil`.
/// Should always be used when accessing properties of this class.
/// - Parameter callback: Closure to call whenever state changes.
public init?(host: String = "google.com", queue: DispatchQueue? = nil, callback: @escaping (State) -> Void) {
/// - Parameter host: Host to use for monitoring reachability.
/// - Parameter queue: Queue on which to process and update network changes. Will create one if `nil`.
/// Should always be used when accessing properties of this class.
/// - Parameter useNewCellMonitor: Whether to use the new cellular monitor introduced in iOS 12.
/// Due to rdar://46873673 this defaults to false to prevent crashes.
/// - Parameter callback: Closure to call whenever state changes.
public init?(host: String = "google.com", queue: DispatchQueue? = nil, useNewCellMonitor: Bool = false,
callback: @escaping (State) -> Void)
{
guard let reachability = SCNetworkReachabilityCreateWithName(nil, host) else {
return nil
}

self.queue = queue ?? DispatchQueue(label: "SwiftGRPC.ClientNetworkMonitor.queue")
self.useNewCellMonitor = useNewCellMonitor
self.callback = callback
self.reachability = reachability
self.startMonitoringReachability(reachability)
Expand All @@ -88,7 +94,7 @@ open class ClientNetworkMonitor {

private func startMonitoringCellular() {
let notificationName: Notification.Name
if #available(iOS 12.0, *) {
if #available(iOS 12.0, *), self.useNewCellMonitor {
notificationName = .CTServiceRadioAccessTechnologyDidChange
} else {
notificationName = .CTRadioAccessTechnologyDidChange
Expand All @@ -102,7 +108,7 @@ open class ClientNetworkMonitor {
private func cellularDidChange(_ notification: NSNotification) {
self.queue.async {
let newCellularName: String?
if #available(iOS 12.0, *) {
if #available(iOS 12.0, *), self.useNewCellMonitor {
let cellularKey = notification.object as? String
newCellularName = cellularKey.flatMap { self.cellularInfo.serviceCurrentRadioAccessTechnology?[$0] }
} else {
Expand Down
Loading