Using SCNetworkReachability callbacks in Objective-C app

  • Hope someone here will be able to help me out...
    I have a regular Objective-C/Cocoa app. In my main Controller class, I have instance variables
    Boolean isReachable;
    SCNetworkReachabilityRef facebookName;

    In my application initialization code I have
    facebookName = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [@"www.facebook.com" cStringUsingEncoding:NSISOLatin1StringEncoding]);

    if (SCNetworkReachabilitySetCallback(facebookName, networkStatusDidChange, NULL) &&
      SCNetworkReachabilityScheduleWithRunLoop(facebookName, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
      CFRunLoopRun();
    }

    Then, here's the callback function:
      void networkStatusDidChange(SCNetworkReachabilityRef name, SCNetworkConnectionFlags flags, void * infoDictionary) {
    if (name != NULL) {
      if (flags != kSCNetworkFlagsReachable) {
      isReachable = NO;
      } else {
      isReachable = YES;
      }
    }
      }
    However, this code won't work due to a compile-time error at the lines where I try to set isReachable: error: 'isReachable' was not declared in this scope.
    What am I doing wrong here?
    Thanks and happy holidays!
    Paul
  • oh. That's a bit of a "duh" moment.
    As a quick a follow-up, is there any particular place this function should be in? Does it need to sit in its own file that will then have to be imported by the class, or can it just be in the class itself?
    ...I really wonder sometimes why Apple makes us deal with two separate programming paradigms to get things done...
    Paul

    ----------Original Message----------
      On 12/26/07 19:49 , David Symonds (<dsymonds...>) wrote:
    > On Dec 26, 2007 7:25 PM, Paul Borokhov <lensovetp...> wrote:
    >
    >> Then, here's the callback function:
    >> void networkStatusDidChange(SCNetworkReachabilityRef name,
    >> SCNetworkConnectionFlags flags, void * infoDictionary) {
    >> if (name != NULL) {
    >> if (flags != kSCNetworkFlagsReachable) {
    >> isReachable = NO;
    >> } else {
    >> isReachable = YES;
    >> }
    >> }
    >> }
    >> However, this code won't work due to a compile-time error at the
    >> lines where I try to set isReachable: error: 'isReachable' was not
    >> declared in this scope.
    >> What am I doing wrong here?
    >
    > The callback function is a *function*, not a class's method. When this
    > code runs, you're "outside" your controller class, so you'll need to
    > call something on your controller class from this function to achieve
    > what you want. That's what the infoDictionary is there for: so you can
    > pass, say, a pointer to your controller object.
    >
    >
    > Dave.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today