1

I know how to do it with Reachability in a portion of code in the app but what I need is to do it in the whole app for example if the user is using my application and gets to the underground where there's no internet connection.

I need to have something that warn the user he's about to lost internet connection or something similar.

This is what I have but I can't copy-paste the same code in every part of the code.

public class Reachability {
class func isConnectedToNetwork() -> Bool {
    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)
    let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
    }
    var flags = SCNetworkReachabilityFlags.ConnectionAutomatic
    if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
        return false
    }
    let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
    let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
    return (isReachable && !needsConnection)
}}
A. Sola
  • 107
  • 1
  • 12
  • I've seen that entry but what I need is something to check continuously to warn the user. – A. Sola Nov 28 '15 at 14:21
  • 2
    You can register a callback which is notified when the reachability status changes, compare http://stackoverflow.com/questions/27142263/working-with-c-apis-from-swift and http://stackoverflow.com/questions/30786883/swift-2-unsafemutablepointervoid-to-object. – All that is already implemented in the https://github.com/ashleymills/Reachability.swift project. – Martin R Nov 28 '15 at 14:24
  • @MartinR That was exactly what I was looking for. Many thanks. – A. Sola Nov 28 '15 at 14:34

0 Answers0