Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
Use Sources and Tests directories.
Browse files Browse the repository at this point in the history
Add a ReachabilityTests target that gets run as part of the Reachability scheme.

Add schemes to run all of the sample apps.

Clean up sample apps a little bit.
  • Loading branch information
hamchapman committed Apr 7, 2018
1 parent 5ab63d8 commit 48cfd70
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 99 deletions.
187 changes: 164 additions & 23 deletions Reachability.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AA73447B1BE7678B008AFE69"
BlueprintIdentifier = "335AD5962052EB22000D4D08"
BuildableName = "ReachabilityTests.xctest"
BlueprintName = "ReachabilityTests"
ReferencedContainer = "container:Reachability.xcodeproj">
Expand Down
1 change: 0 additions & 1 deletion ReachabilityAppleTVSample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}
}

35 changes: 17 additions & 18 deletions ReachabilityAppleTVSample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder.AppleTV.Storyboard" version="3.0" toolsVersion="9060" systemVersion="15B42" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.AppleTV.Storyboard" version="3.0" toolsVersion="13771" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="appleTV" orientation="landscape">
<adaptation id="light"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
<deployment identifier="tvOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -17,33 +22,27 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Jew-4p-o9x">
<rect key="frame" x="695" y="408" width="530" height="91"/>
<animations/>
<rect key="frame" x="924" y="468" width="72" height="91"/>
<constraints>
<constraint firstAttribute="width" constant="530" id="MBv-Rz-Tzd"/>
<constraint firstAttribute="height" constant="91" id="dMv-aK-0ih"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="30"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wE4-fM-BF3">
<rect key="frame" x="695" y="507" width="530" height="165"/>
<animations/>
<rect key="frame" x="906" y="567" width="108" height="55"/>
<fontDescription key="fontDescription" type="system" pointSize="46"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<animations/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="wE4-fM-BF3" secondAttribute="bottom" constant="408" id="BDI-ry-4pH"/>
<constraint firstItem="wE4-fM-BF3" firstAttribute="trailing" secondItem="Jew-4p-o9x" secondAttribute="trailing" id="GGb-7m-6uX"/>
<constraint firstItem="Jew-4p-o9x" firstAttribute="leading" secondItem="wE4-fM-BF3" secondAttribute="leading" id="Q1c-Jb-iKN"/>
<constraint firstItem="wE4-fM-BF3" firstAttribute="top" secondItem="Jew-4p-o9x" secondAttribute="bottom" constant="8" symbolic="YES" id="U8V-qN-EPr"/>
<constraint firstItem="Jew-4p-o9x" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Vng-pT-hW8"/>
<constraint firstItem="Jew-4p-o9x" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="408" id="cMK-9n-7pq"/>
<constraint firstItem="Jew-4p-o9x" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="McL-OC-xmz"/>
<constraint firstItem="wE4-fM-BF3" firstAttribute="top" secondItem="Jew-4p-o9x" secondAttribute="bottom" constant="8" id="TRM-vy-Pvk"/>
<constraint firstItem="Jew-4p-o9x" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="408" id="j6d-xn-evu"/>
<constraint firstItem="wE4-fM-BF3" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="ne8-Vk-QZp"/>
</constraints>
</view>
<connections>
Expand Down
9 changes: 6 additions & 3 deletions ReachabilityAppleTVSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class ViewController: UIViewController {
self.updateLabelColourWhenNotReachable(reachability)
}
} else {
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: .reachabilityChanged, object: reachability)
NotificationCenter.default.addObserver(
self,
selector: #selector(ViewController.reachabilityChanged(_:)),
name: .reachabilityChanged,
object: reachability
)
}
}

Expand Down Expand Up @@ -108,6 +113,4 @@ class ViewController: UIViewController {
deinit {
stopNotifier()
}

}

1 change: 0 additions & 1 deletion ReachabilityMacSample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
}
}

7 changes: 6 additions & 1 deletion ReachabilityMacSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class ViewController: NSViewController {
self.updateLabelColourWhenNotReachable(reachability)
}
} else {
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: .reachabilityChanged, object: reachability)
NotificationCenter.default.addObserver(
self,
selector: #selector(ViewController.reachabilityChanged(_:)),
name: .reachabilityChanged,
object: reachability
)
}
}

Expand Down
41 changes: 17 additions & 24 deletions ReachabilitySample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9060" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina5_9" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,39 +18,29 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="812"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="UBQ-2c-x8L">
<rect key="frame" x="40" y="274" width="520" height="53"/>
<animations/>
<rect key="frame" x="136" y="379.66666666666669" width="103" height="53"/>
<fontDescription key="fontDescription" type="system" pointSize="44"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5EE-X2-pKr">
<rect key="frame" x="276" y="242" width="49" height="24"/>
<animations/>
<rect key="frame" x="163.66666666666666" y="242" width="48" height="24"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="UBQ-2c-x8L" firstAttribute="top" secondItem="5EE-X2-pKr" secondAttribute="bottom" constant="8" symbolic="YES" id="CuO-ho-b10"/>
<constraint firstAttribute="centerY" secondItem="UBQ-2c-x8L" secondAttribute="centerY" id="K7q-cv-Lfi"/>
<constraint firstItem="5EE-X2-pKr" firstAttribute="centerX" secondItem="UBQ-2c-x8L" secondAttribute="centerX" id="MZr-wz-ELn"/>
<constraint firstItem="UBQ-2c-x8L" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="20" id="YBI-sa-7Z9"/>
<constraint firstItem="5EE-X2-pKr" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="225" id="pMO-lY-Wq8"/>
<constraint firstAttribute="trailingMargin" secondItem="UBQ-2c-x8L" secondAttribute="trailing" constant="20" id="xcC-HE-ZUz"/>
<constraint firstItem="UBQ-2c-x8L" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="3mq-UY-zBf"/>
<constraint firstItem="5EE-X2-pKr" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="OZH-vM-adg"/>
<constraint firstItem="5EE-X2-pKr" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="198" id="OnQ-cQ-2GV"/>
<constraint firstItem="UBQ-2c-x8L" firstAttribute="top" secondItem="5EE-X2-pKr" secondAttribute="bottom" constant="113.66666666666669" id="PpX-WU-7UN"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="pMO-lY-Wq8"/>
</mask>
</variation>
</view>
<connections>
<outlet property="hostNameLabel" destination="5EE-X2-pKr" id="qt8-ZE-mUg"/>
Expand Down
13 changes: 7 additions & 6 deletions ReachabilitySample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class ViewController: UIViewController {
self.updateLabelColourWhenNotReachable(reachability)
}
} else {
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:)), name: .reachabilityChanged, object: reachability)
NotificationCenter.default.addObserver(
self,
selector: #selector(reachabilityChanged(_:)),
name: .reachabilityChanged,
object: reachability
)
}
}

Expand Down Expand Up @@ -93,8 +98,7 @@ class ViewController: UIViewController {

self.networkStatus.text = "\(reachability.connection)"
}



@objc func reachabilityChanged(_ note: Notification) {
let reachability = note.object as! Reachability

Expand All @@ -108,7 +112,4 @@ class ViewController: UIViewController {
deinit {
stopNotifier()
}

}


File renamed without changes.
File renamed without changes.
27 changes: 12 additions & 15 deletions Reachability/Reachability.swift → Sources/Reachability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ extension Notification.Name {
public static let reachabilityChanged = Notification.Name("reachabilityChanged")
}

func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {

func callback(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
guard let info = info else { return }

let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
Expand Down Expand Up @@ -101,7 +100,6 @@ public class Reachability {
}

public var connection: Connection {

guard isReachableFlagSet else { return .none }

// If we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
Expand Down Expand Up @@ -144,21 +142,21 @@ public class Reachability {
fileprivate let reachabilityRef: SCNetworkReachability

fileprivate let reachabilitySerialQueue = DispatchQueue(label: "uk.co.ashleymills.reachability")

fileprivate var usingHostname = false

required public init(reachabilityRef: SCNetworkReachability) {
required public init(reachabilityRef: SCNetworkReachability, usingHostname: Bool = false) {
allowsCellularConnection = true
self.reachabilityRef = reachabilityRef
self.usingHostname = usingHostname
}

public convenience init?(hostname: String) {

guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else { return nil }

self.init(reachabilityRef: ref)
self.init(reachabilityRef: ref, usingHostname: true)
}

public convenience init?() {

var zeroAddress = sockaddr()
zeroAddress.sa_len = UInt8(MemoryLayout<sockaddr>.size)
zeroAddress.sa_family = sa_family_t(AF_INET)
Expand All @@ -177,7 +175,6 @@ public extension Reachability {

// MARK: - *** Notifier methods ***
func startNotifier() throws {

guard !notifierRunning else { return }

var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
Expand All @@ -196,7 +193,7 @@ public extension Reachability {
reachabilitySerialQueue.async {
self.reachabilityChanged()
}

notifierRunning = true
}

Expand All @@ -210,7 +207,6 @@ public extension Reachability {
// MARK: - *** Connection test methods ***
@available(*, deprecated: 4.0, message: "Please use `connection != .none`")
var isReachable: Bool {

guard isReachableFlagSet else { return false }

if isConnectionRequiredAndTransientFlagSet {
Expand All @@ -235,7 +231,6 @@ public extension Reachability {

@available(*, deprecated: 4.0, message: "Please use `connection == .wifi`")
var isReachableViaWiFi: Bool {

// Check we're reachable
guard isReachableFlagSet else { return false }

Expand All @@ -247,7 +242,6 @@ public extension Reachability {
}

var description: String {

let W = isRunningOnDevice ? (isOnWWANFlagSet ? "W" : "-") : "X"
let R = isReachableFlagSet ? "R" : "-"
let c = isConnectionRequiredFlagSet ? "c" : "-"
Expand All @@ -263,13 +257,15 @@ public extension Reachability {
}

fileprivate extension Reachability {

func reachabilityChanged() {
guard previousFlags != flags else { return }

let block = connection != .none ? whenReachable : whenUnreachable

DispatchQueue.main.async {
if self.usingHostname {
print("USING HOSTNAME ABOUT TO CALL BLOCK")
}
block?(self)
self.notificationCenter.post(name: .reachabilityChanged, object:self)
}
Expand Down Expand Up @@ -318,6 +314,7 @@ fileprivate extension Reachability {
var flags: SCNetworkReachabilityFlags {
var flags = SCNetworkReachabilityFlags()
if SCNetworkReachabilityGetFlags(reachabilityRef, &flags) {
print("Returning flags \(flags)")
return flags
} else {
return SCNetworkReachabilityFlags()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class ReachabilityTests: XCTestCase {
}

func testValidHost() {

// Testing with an invalid host will initially show as UNreachable, but then the callback
// gets fired a second time reporting the host as reachable


let validHostName = "google.com"

guard let reachability = Reachability(hostname: validHostName) else {
Expand Down Expand Up @@ -61,6 +56,9 @@ class ReachabilityTests: XCTestCase {
}

func testInvalidHost() {
// Testing with an invalid host will initially show as reachable, but then the callback
// gets fired a second time reporting the host as unreachable

let invalidHostName = "invalidhost"

guard let reachability = Reachability(hostname: invalidHostName) else {
Expand Down Expand Up @@ -93,5 +91,5 @@ class ReachabilityTests: XCTestCase {

reachability.stopNotifier()
}

}

0 comments on commit 48cfd70

Please sign in to comment.