Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check for local Wi-Fi (not just cellular connection) using iPhone SDK?
    primarykey
    data
    text
    <p>I'm currently using the following to check whether <a href="http://en.wikipedia.org/wiki/Wi-Fi" rel="noreferrer">Wi-Fi</a> is available for my application:</p> <pre><code>#import &lt;SystemConfiguration/SystemConfiguration.h&gt; static inline BOOL addressReachable(const struct sockaddr_in *hostAddress); BOOL localWiFiAvailable() { struct sockaddr_in localWifiAddress; bzero(&amp;localWifiAddress, sizeof(localWifiAddress)); localWifiAddress.sin_len = sizeof(localWifiAddress); localWifiAddress.sin_family = AF_INET; // IN_LINKLOCALNETNUM is defined in &lt;netinet/in.h&gt; as 169.254.0.0 localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); return addressReachable(&amp;localWifiAddress); } static inline BOOL addressReachable(const struct sockaddr_in *hostAddress) { const SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress); if (target != NULL) { SCNetworkReachabilityFlags flags = 0; const BOOL reachable = SCNetworkReachabilityGetFlags(target, &amp;flags); CFRelease(target); return reachable &amp;&amp; (flags &amp; kSCNetworkFlagsReachable); } return NO; } </code></pre> <p>This, however, does not return NO as it should when the iPhone is connected only to a cellular network but not a Wi-Fi network. Does anyone know how to fix this?</p> <h2>Edit</h2> <p>So this is what I ended up using:</p> <pre><code>#import &lt;arpa/inet.h&gt; // For AF_INET, etc. #import &lt;ifaddrs.h&gt; // For getifaddrs() #import &lt;net/if.h&gt; // For IFF_LOOPBACK BOOL localWiFiAvailable() { struct ifaddrs *addresses; struct ifaddrs *cursor; BOOL wiFiAvailable = NO; if (getifaddrs(&amp;addresses) != 0) return NO; cursor = addresses; while (cursor != NULL) { if (cursor -&gt; ifa_addr -&gt; sa_family == AF_INET &amp;&amp; !(cursor -&gt; ifa_flags &amp; IFF_LOOPBACK)) // Ignore the loopback address { // Check for WiFi adapter if (strcmp(cursor -&gt; ifa_name, "en0") == 0) { wiFiAvailable = YES; break; } } cursor = cursor -&gt; ifa_next; } freeifaddrs(addresses); return wiFiAvailable; } </code></pre> <p>Thanks "unforgiven" (and Matt Brown apparently).</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.
 

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