1

I need to know if the device is connected via WIFI or not. This should be pretty simple, but I broke my neck on the sample code apple supply HERE. I can't seem to get it to work in my own app. Is this not the only thing I need to do?

IN H:

#import <UIKit/UIKit.h>    
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>

    @class Reachability;

    @interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {

        Reachability* wifiReach;
    }

IN M: I just try to call the following code in viewDidLoad:

wifiReach = [[Reachability reachabilityForLocalWiFi] retain];

But compiling results in:

WARNING: no '+reachabilityForLocalWiFi' method found

ERROR: "_OBJC_CLASS_$_Reachability", referenced from: objc-class-ref-to-Reachability in FirstViewController.o - Symbol not found

Seeing this, I'm probably doing something really wrong here. Thought this would be a simple task. Damn my good ideas.

John Kofod
  • 196
  • 1
  • 13

2 Answers2

4
#import "Reachability.h"

and

- (BOOL)networkCheck{
    Reachability *curReach = [[Reachability reachabilityForInternetConnection] retain];
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    [curReach release];
    switch (netStatus)
    {
        case NotReachable:
        {
            NSLog(@"NETWORKCHECK: Not Connected");
            return false;
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"NETWORKCHECK: Connected Via WWAN");
            return false;
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"NETWORKCHECK: Connected Via WiFi");
            return true;
            break;
        } 
    }
    return false;
}

then [self networkCheck] will return true if connected to wifi. I use the reachability code too, and this works perfectly in all my applications.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Ups. You are absolutely right. I forgot to import the header, so of cause Xcode couldn't figure things out. Now it works. Btw, isn't there a leak in your code. Where does curReach gets released?? – John Kofod Nov 04 '10 at 20:05
  • oops... :p *looks embarassed* – Thomas Clayson Nov 04 '10 at 20:09
  • to be honest I'm not sure why its even retained in the first place. I'm sure there was a good reason at the time. :/ – Thomas Clayson Nov 04 '10 at 20:10
  • Or is it released somewhere? If I autorelease it, the app crashes. Have you tried it without the first retain? Or with the code you have written now? – John Kofod Nov 05 '10 at 07:42
  • if you just do `Reachability *curReach = [Reachability reachabilityForInternetConnection];` without the retain what happens then? That will be autoreleased automatically, so you don't have to release it then. – Thomas Clayson Nov 06 '10 at 11:07
2

Do you have:

#import "Reachability.h"

in your .m file?

jmans
  • 5,648
  • 4
  • 27
  • 32