Skip to content

Instantly share code, notes, and snippets.

@ValCanBuild
Last active January 25, 2016 23:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ValCanBuild/0d9402fdb70bbda68352 to your computer and use it in GitHub Desktop.
Save ValCanBuild/0d9402fdb70bbda68352 to your computer and use it in GitHub Desktop.
Constrained View Controller extension which displays a "No Internet Connection" message when reachability is lost. To use it simply extend your existing ViewController and make it implement ReachabilityAware and call the startMonitoryReachability() method.
// Constrained View Controller extension which display a "No Internet Connection" message when reachability is lost.
// This implementation depends on ReachabilitySwift (https://github.com/ashleymills/Reachability.swift).
// It can easily enough be adapted to other frameworks.
// Assumes you have a localized string with the key "warning.no_internet_connection" in one of your .strings files.
// Call startMonitoringReachability() in viewWillAppear and stopMonitoringReachability() in viewDidDisappear
import ReachabilitySwift
protocol ReachabilityAware {
func startMonitoringReachability()
func stopMonitoringReachability()
}
extension ReachabilityAware where Self: UIViewController {
private var reachability: Reachability? {
get {
if let reach = objc_getAssociatedObject(self, &AssociatedKeys.ReachabilityKey) as? Reachability {
return reach
}
do {
let reach = try Reachability.reachabilityForInternetConnection()
objc_setAssociatedObject(self, &AssociatedKeys.ReachabilityKey, reach, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return reach
}
catch let error {
print("Error creating reachability \(error)")
return nil
}
}
}
func startMonitoringReachability() {
reachability?.whenUnreachable = { reachability in
self.becameUnreachable()
}
reachability?.whenReachable = { reachability in
self.becameReachable()
}
do {
try reachability?.startNotifier()
} catch let error {
print("Unable to start reachability notifier with error \(error)")
}
let isInitiallyReachable = reachability?.isReachable() ?? false
isInitiallyReachable ? becameReachable() : becameUnreachable()
}
func stopMonitoringReachability() {
reachability?.stopNotifier()
}
private func becameUnreachable() {
if self.view.viewWithTag(Tags.NoInternetViewTag) != nil {
return
}
let label = UILabel()
label.text = NSLocalizedString("warning.no_internet_connection", comment: "No Internet Connection")
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.blackColor()
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(12.0)
label.tag = Tags.NoInternetViewTag
self.view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints([
NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)
])
label.addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20))
}
private func becameReachable() {
if let label = self.view.viewWithTag(Tags.NoInternetViewTag) {
UIView.animateWithDuration(0.25,
animations: { _ in
label.transform = CGAffineTransformMakeTranslation(0, label.frame.height)
},
completion: { complete in
if complete {
label.removeFromSuperview()
}
})
}
}
}
private struct Tags {
static var NoInternetViewTag = 123
}
private struct AssociatedKeys {
static var ReachabilityKey = "reachability_key"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment