1

Is there a notification that is dispatched when a user's device suddenly connects to wifi? Or something of this nature?

I'm wanted a certain selector/method in my app delegate to run whenever a user connects to wireless. How do I know when to perform my selector?

** EDIT **

I ended up finding an IOS 5 ARC friendly version of Reachability if anyone needs it.

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 2
    check this project from apple: http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html – CarlJ Apr 18 '12 at 13:40

1 Answers1

2

You should include the Reachability header and implementation from here.

I did a project a while ago where I needed to check if I was connected to WiFi at certain times. Here's some code that may be useful to you:

- (BOOL) connectedToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags)
    {
        printf("Error. Could not recover network reachability flags\n");
        return 0;
    }

    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
    return ((isReachable && !needsConnection) && !(nonWiFi)) ? YES : NO;
}

It returns true if connected to WiFi.

One way to detect when a change occurs would be to run an NSTimer and check if the result changes over time. You wouldn't want to run it too often though.

Community
  • 1
  • 1
Liam George Betsworth
  • 18,373
  • 5
  • 39
  • 42