Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, modify your addressReachable method. Instead of </p> <pre><code>return reachable &amp;&amp; (flags &amp; kSCNetworkFlagsReachable); </code></pre> <p>add the following:</p> <pre><code>BOOL isReachable = ((flags &amp; kSCNetworkFlagsReachable) != 0); BOOL needsConnection = ((flags &amp; kSCNetworkFlagsConnectionRequired) != 0); return (isReachable &amp;&amp; !needsConnection) ? YES : NO; </code></pre> <p>This is the proper way to check for a connection available. Now, if you want to clearly distinguish between cellular and wifi, modify your method to return an int and use the following</p> <pre><code>BOOL isReachable = ((flags &amp; kSCNetworkFlagsReachable) != 0); BOOL needsConnection = ((flags &amp; kSCNetworkFlagsConnectionRequired) != 0); if(isReachable &amp;&amp; !needsConnection) // connection is available { // determine what type of connection is available BOOL isCellularConnection = ((flags &amp; kSCNetworkReachabilityFlagsIsWWAN) != 0); NSString *wifiIPAddress = [self getWiFiIPAddress]; if(isCellularConnection) return 1; // cellular connection available if(wifiIPAddress) return 2; // wifi connection available } else return 0; // no connection at all </code></pre> <p>The getWiFiIPAddress method is courtesy of Matt Brown, and can be found <a href="http://mattbsoftware.blogspot.com/2009/04/how-to-get-ip-address-of-iphone-os-v221.html" rel="noreferrer">here</a>.</p> <p>One more thing. The kSCNetworkReachabilityFlagsIsDirect flag can tell you whether the network traffic goes through a gateway or arrives directly. This may be helpful in some cases.</p> <p>The code works correctly on the device. On the simulator, it will declare that you are connected trough wifi when you are connected through the ethernet cable, and will declare no connection if you are connected through wifi.</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.
    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