Create Reachability Using Swift 3 XCode 8

Source Code

import Foundation
import SystemConfiguration

//Create Queue for Reachability Callback
fileprivate let reachabilitySerialQueue = DispatchQueue(label: “com.kapilahuja.reachability.reachability”)

//Variable to save Reachability status and show toast if not reachable
var isInternetReachable = false {
didSet {
if !isInternetReachable {
ApplicationManager.sharedInstance.ShowInternetAlertToast()
}
}
}

/* Check Connectivity */
func isInternetAvailable()
{
// Create Network Configuration & Flag
// —– Start —–
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}

var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
isInternetReachable = false
return
}
// —– End —–

// Set Network Reachability Callback on status change of route & set queue for it
// —– Start —–
var context = SCNetworkReachabilityContext()
if !SCNetworkReachabilitySetCallback(defaultRouteReachability!, callback, &context) {
print(SCError())
}

if !SCNetworkReachabilitySetDispatchQueue(defaultRouteReachability!, reachabilitySerialQueue) {
print(SCError())
}
// —– End —–

// Get Network Status & Save it
// —– Start —–
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

isInternetReachable = (isReachable && !needsConnection)
// —– End —–
}

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

//guard info != nil else { return }

// Get Network Status & Save it
// —– Start —–
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

isInternetReachable = (isReachable && !needsConnection)
// —– End —–
}

Images

Screen Shot 2017-05-31 at 3.00.03 PM

Screen Shot 2017-05-31 at 3.00.14 PM

How to use

Initialize

//Check Connectivity
isInternetAvailable()

Use

if isInternetReachable {

}

Create Reachability Using Swift 3 XCode 8

Create Designable Subclass of UIView To Create Gradient Layer on Draw View

Source Code

// MARK: – Designable GradientView Class
@IBDesignable final class GradientView: UIView {

@IBInspectable var startColor: UIColor = UIColor.init(colorLiteralRed: 158.0/255.0, green: 201.0/255.0, blue: 250.0/255.0, alpha: 1.0)
@IBInspectable var endColor: UIColor = UIColor.white

/* Initiate the view with Radial Gradient filled with defined colors */
override func draw(_ rect: CGRect) {

// Setup view
let colors = [endColor.cgColor, startColor.cgColor] as CFArray
let locations = [ 0.0, 1.0 ] as [CGFloat]
let radius = min((self.bounds.size.height + 50), (self.bounds.size.width + 50))
let center = CGPoint.init(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)

// Prepare a context and create a color space
let context = UIGraphicsGetCurrentContext()
context!.saveGState()
let colorSpace = CGColorSpaceCreateDeviceRGB()

// Create gradient object from our color space, color components and locations
let gradient = CGGradient.init(colorsSpace: colorSpace, colors: colors, locations: locations)

// Draw a gradient
context!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: radius, options: CGGradientDrawingOptions(rawValue: 0))
context?.restoreGState()
}

}

Use

Create Designable Subclass of UIView To Create Gradient Layer on Draw View