Skip to content

Commit

Permalink
Release 0.0.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Butler committed Jul 17, 2017
0 parents commit 165d02e
Show file tree
Hide file tree
Showing 56 changed files with 3,231 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# OS X
.DS_Store

# Xcode
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/
*.xccheckout
profile
*.moved-aside
DerivedData
*.hmap
*.ipa

# Bundler
.bundle

Carthage
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Note: if you ignore the Pods directory, make sure to uncomment
# `pod install` in .travis.yml
#
# Pods/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# references:
# * http://www.objc.io/issue-6/travis-ci.html
# * https://github.com/supermarin/xcpretty#usage

osx_image: xcode7.3
language: objective-c
# cache: cocoapods
# podfile: Example/Podfile
# before_install:
# - gem install cocoapods # Since Travis is not always on latest version
# - pod install --project-directory=Example
script:
- set -o pipefail && xcodebuild test -workspace Example/Connectivity.xcworkspace -scheme Connectivity-Example -sdk iphonesimulator9.3 ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
Binary file added Connectivity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions Connectivity.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Be sure to run `pod lib lint Connectivity.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'Connectivity'
s.version = '0.0.1'
s.summary = 'Wraps Reachability to help developers determine where Internet connectivity is available'

# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!

s.description = <<-DESC
Connectivity is a wrapper for Reachability which provides a true indication of whether Internet connectivity is available. Connectivity's objective is to solve the captive portal problem whereby a device running iOS is connected to a WiFi network lacking Internet connectivity. Connectivity can detect such situations enabling you to react accordingly.
DESC

s.homepage = 'https://github.com/rwbutler/Connectivity'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'rwbutler' => '[email protected]' }
s.source = { :git => 'https://github.com/rwbutler/Connectivity.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

s.source_files = 'Connectivity/Classes/**/*'

# s.resource_bundles = {
# 'Connectivity' => ['Connectivity/Assets/*.png']
# }

# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
Empty file added Connectivity/Assets/.gitkeep
Empty file.
Empty file added Connectivity/Classes/.gitkeep
Empty file.
209 changes: 209 additions & 0 deletions Connectivity/Classes/Connectivity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//
// Connectivity.swift
// Connectivity
//
// Created by Ross Butler on 7/12/17.
// Copyright © 2017 Ross Butler. All rights reserved.
//

extension Notification.Name {
static let ReachabilityDidChange = Notification.Name("kNetworkReachabilityChangedNotification")
static let ConnectivityDidChange = Notification.Name("kNetworkConnectivityChangedNotification")
}

public class Connectivity {
public struct Percentage {
let value: Double
init?(_ value: Double) {
guard (0.0...100.0).contains(value) else {
return nil
}
self.value = value
}
}
public typealias NetworkConnected = (Connectivity) -> ()
public typealias NetworkDisconnected = (Connectivity) -> ()

public private(set) var isConnected: Bool = false
public static var connectivityThreshold: Connectivity.Percentage = Connectivity.Percentage(75.0)!
public static var connectivityURLs: [URL] = {
var result: [URL] = []
var useHTTP = false
if let bundleInfo = Bundle.main.infoDictionary,
let appTransportSecurity = bundleInfo["NSAppTransportSecurity"] as? [String: Any],
let allowsArbitraryLoads = appTransportSecurity["NSAllowsArbitraryLoads"] as? Bool {
useHTTP = allowsArbitraryLoads
}
let connectivityDomains: [String] = (useHTTP)
? [
"www.apple.com",
"apple.com",
"www.appleiphonecell.com",
"www.itools.info",
"www.ibook.info",
"www.airport.us",
"www.thinkdifferent.us"
]
: [ "www.apple.com" ] // Recommended supplementing with your own URLs
let connectivityPath = "/library/test/success.html"
let httpProtocol = (useHTTP) ? "http" : "https"
for domain in connectivityDomains {
if let connectivityURL = URL(string: "\(httpProtocol)://\(domain)\(connectivityPath)") {
result.append(connectivityURL)
}
}
return result
}()
public static var urlSessionConfiguration: URLSessionConfiguration = {
let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.timeoutIntervalForRequest = 5.0
sessionConfiguration.timeoutIntervalForResource = 5.0
return sessionConfiguration
}()
public var notificationCenter: NotificationCenter = NotificationCenter.default

public enum ConnectivityStatus: CustomStringConvertible {
case notConnected, connectedViaWiFi, connectedViaWWAN, connectedViaWiFiWithoutInternet, connectedViaWWANWithoutInternet

public var description: String {
switch self {
case .connectedViaWWAN: return "Cellular"
case .connectedViaWWANWithoutInternet: return "Cellular without Internet access"
case .connectedViaWiFi: return "WiFi"
case .connectedViaWiFiWithoutInternet: return "WiFi without Internet access"
case .notConnected: return "No Connection"
}
}
}
public var whenConnected: NetworkConnected?
public var whenDisconnected: NetworkDisconnected?

public var currentConnectivityString: String {
return "\(currentConnectivityStatus)"
}

public var currentConnectivityStatus: ConnectivityStatus {
if isConnectedViaWiFi {
return .connectedViaWiFi
}
if isConnectedViaWWAN
{
return .connectedViaWWAN
}
if isConnectedViaWiFiWithoutInternet {
return .connectedViaWiFiWithoutInternet
}
if isConnectedViaWWANWithoutInternet
{
return .connectedViaWWANWithoutInternet
}
return .notConnected
}
let reachability: Reachability
private static let expectedResponse = "Success"
private var timer: Timer? = nil
fileprivate var reachabilityPolling = false
public var aggressivePolling: Bool = false {
didSet {
aggressivePolling(enabled: aggressivePolling)
}
}

public func aggressivePolling(enabled: Bool) {
if #available(iOS 10.0, *) {
timer?.invalidate()
if aggressivePolling && reachabilityPolling {
timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { timer in
self.performConnectivityChecks()
})
}
}
}

public init() {
reachability = Reachability.forInternetConnection()
performConnectivityChecks()
}

public func performConnectivityChecks() {
let connectivityURLs = type(of: self).connectivityURLs
let connectivityDomainCount: Double = Double(connectivityURLs.count)
var successfulConnectivityChecks: Double = 0
var failedConnectivityChecks: Double = 0

for connectivityURL in connectivityURLs {
let urlSession = URLSession(configuration: type(of: self).urlSessionConfiguration)
let task = urlSession.dataTask(with: connectivityURL, completionHandler: { [weak self] (data, response, error) in
guard let strongSelf = self else {
return
}
if let data = data,
let responseString = String(data: data, encoding: .utf8),
responseString.contains(type(of: strongSelf).expectedResponse) {
successfulConnectivityChecks += 1
} else {
failedConnectivityChecks += 1
}

if connectivityDomainCount == (successfulConnectivityChecks + failedConnectivityChecks) {
let percentageSuccessful = (successfulConnectivityChecks / connectivityDomainCount) * 100.0
strongSelf.isConnected = (percentageSuccessful >= type(of: strongSelf).connectivityThreshold.value)
? true : false
unowned let unownedSelf = strongSelf // Caller responsible for maintaining the reference
strongSelf.notificationCenter.post(name: .ConnectivityDidChange, object: unownedSelf)
DispatchQueue.main.async {
let callback = strongSelf.isConnected ? strongSelf.whenConnected : strongSelf.whenDisconnected
callback?(unownedSelf)
}
}
})
task.resume()
}
}

@objc fileprivate func reachabilityDidChange(_ notification: NSNotification) {
performConnectivityChecks()
}

deinit {
stopNotifier()
}
}

public extension Connectivity {
var isConnectedViaWWAN: Bool {
return isConnected && reachability.currentReachabilityStatus() == ReachableViaWWAN
}

var isConnectedViaWiFi: Bool {
return isConnected && reachability.currentReachabilityStatus() == ReachableViaWiFi
}

var isConnectedViaWWANWithoutInternet: Bool {
return reachability.currentReachabilityStatus() == ReachableViaWWAN
}

var isConnectedViaWiFiWithoutInternet: Bool {
return reachability.currentReachabilityStatus() == ReachableViaWiFi
}

var description: String {
return "\(reachability.description)"
}

func startNotifier() {
reachability.startNotifier()
reachabilityPolling = true
aggressivePolling(enabled: aggressivePolling)
NotificationCenter.default.addObserver(self,
selector: #selector(reachabilityDidChange(_:)),
name: NSNotification.Name.ReachabilityDidChange,
object: nil)
}

func stopNotifier() {
reachability.stopNotifier()
reachabilityPolling = false
notificationCenter.removeObserver(self)
}
}
42 changes: 42 additions & 0 deletions Connectivity/Classes/Reachability/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Sample code project: Reachability
Version: 5.0

IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2016 Apple Inc. All Rights Reserved.
Loading

0 comments on commit 165d02e

Please sign in to comment.