Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Important</strong>: This check should <em>always</em> be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.</p> <hr> <h1>Swift</h1> <p>1) Install via CocoaPods or Carthage: <a href="https://github.com/ashleymills/Reachability.swift" rel="noreferrer">https://github.com/ashleymills/Reachability.swift</a></p> <p>2) Test reachability via closures</p> <pre><code>let reachability = Reachability()! reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } reachability.whenUnreachable = { _ in print("Not reachable") } do { try reachability.startNotifier() } catch { print("Unable to start notifier") } </code></pre> <hr> <h1>Objective-C</h1> <p>1) Add <code>SystemConfiguration</code> framework to the project but don't worry about including it anywhere</p> <p>2) Add Tony Million's version of <code>Reachability.h</code> and <code>Reachability.m</code> to the project (found here: <a href="https://github.com/tonymillion/Reachability" rel="noreferrer">https://github.com/tonymillion/Reachability</a>)</p> <p>3) Update the interface section</p> <pre><code>#import "Reachability.h" // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end </code></pre> <p>4) Then implement this method in the .m file of your view controller which you can call</p> <pre><code>// Checks if we have an internet connection or not - (void)testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }; // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); }; [internetReachableFoo startNotifier]; } </code></pre> <p><strong>Important Note:</strong> The <code>Reachability</code> class is one of the most used classes in projects so you might run into naming conflicts with other projects. If this happens, you'll have to rename one of the pairs of <code>Reachability.h</code> and <code>Reachability.m</code> files to something else to resolve the issue.</p> <p><strong>Note:</strong> The domain you use doesn't matter. It's just testing for a gateway to any domain.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload