1

I want to use the ReachabilitySwift class in my application. I added the dependency in my podFile and I get the code from github.

I got this error:

cannot invoke initializer for type 'Reachability' with no argument" on this line

My code:

let reachability = Reachability()!

Can anyone help me please?

vaibhav
  • 4,038
  • 1
  • 21
  • 51
Ne AS
  • 1,490
  • 3
  • 26
  • 58

2 Answers2

1
let reachability = Reachability(hostname: "http://whatYouWantToCheck.com")

or try a pod update

example from github

let reachability = Reachability()!

reachability.whenReachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    dispatch_async(dispatch_get_main_queue()) {
        if reachability.isReachableViaWiFi() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
}
Daniel Poulsen
  • 1,446
  • 13
  • 21
  • Thank you for your answer but is the reachability test made for network (in general) or for a specific link? – Ne AS Sep 21 '16 at 14:54
  • in the old ver. you had to use this func reachabilityForInternetConnection() but in the new one you can just use Reachability() try and update your lib it might not be up to date – Daniel Poulsen Sep 21 '16 at 15:02
  • I updated it but I have the same problem. Can you give me please a correct example from what I can check if the network is available or not (without using a hostname)? – Ne AS Sep 21 '16 at 15:20
  • I use the same code, but the build failed because of the error given in my question – Ne AS Sep 21 '16 at 15:33
1

Thanks to the answer in this link and also this one, my project is running perfectly and my problem is solved.

By doing this: var reachability: Reachability! I can check the network reachability without using any host for the check.

Community
  • 1
  • 1
Ne AS
  • 1,490
  • 3
  • 26
  • 58