0

I'm a beginner in Swift and I'm trying to make an alert for user to retry connecting to internet. I'm using Reachability by ashleymills. I'm confused on what to put on my alert handler since this is not working:

func alertConnect() -> UIViewController{
let reach = Reachability()
let alert = UIAlertController(title: "No Internet", message: "Tap to retry connect internet", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: {
    action in
    do {
        try reach?.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}))
alert.addAction(UIAlertAction(title: Constants.cancel, style: .cancel))
return alert
}

Here's my Reachability.class:

class ConnectionManager: NSObject {

var reachability: Reachability!

static let sharedInstance: ConnectionManager = { return ConnectionManager() }()


override init() {
    super.init()

    reachability = Reachability()!

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(networkStatusChanged(_:)),
        name: .reachabilityChanged,
        object: reachability
    )

    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}

@objc func networkStatusChanged(_ notification: Notification) {
    // Do something globally here!
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}

static func stopNotifier() -> Void {
    do {
        try (ConnectionManager.sharedInstance.reachability).startNotifier()
    } catch {
        print("Error stopping notifier")
    }
}

static func isReachable(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection != .none {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isUnreachable(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .none {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isReachableViaWWAN(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .cellular {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isReachableViaWiFi(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .wifi {
        completed(ConnectionManager.sharedInstance)
    }
}    }

What should I put on my Retry action handler to let the user reconnect to internet? Thanks for any help.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
faye
  • 1
  • 5
  • Reachability only tells if something is reachable and when the state changes - if it's not, then there is little you can do - Tell the user to check their WiFi and cellular settings – MadProgrammer Aug 28 '18 at 03:15
  • Is there any way where the app can check the connection again once the user clicks a button in an uialertcontroller? @MadProgrammer – faye Aug 28 '18 at 06:46
  • Re-use the reachability API again - but if you've subscribed to it, it will send you notifications – MadProgrammer Aug 28 '18 at 06:49

2 Answers2

0

Working code for Swift 4

import SystemConfiguration

func InternetCheck () -> Bool {

    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) {
        return false
    }
    let isReachable = flags.contains(.reachable)
    let needsConnection = flags.contains(.connectionRequired)
    return (isReachable && !needsConnection)
}

For Check

if InternetCheck() == false {

   let alert = UIAlertView(title:"Uhhhh :(",message: "No internet connection.",delegate: nil ,cancelButtonTitle: "Ok")
   alert.show()
   return

}
// Internet Connection Available
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
0

I found a solution on my problem. Since Reachability checks if the the app is reachable and if there's a network status change, what I did on the Retry handler is declare the function on my view controller which calls the API again while checking the network connection.

faye
  • 1
  • 5