• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Checking for Internet Connection in an iApp
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Checking for Internet Connection in an iApp


  • Subject: Checking for Internet Connection in an iApp
  • From: John Love <email@hidden>
  • Date: Sun, 16 Oct 2011 17:45:05 -0400

I really have been just about everywhere, e.g., stack overflow.com and iphonedevsdk.com and have really learned a lot … unfortunately, what I have learned is that there are opposing opinions on how to best check for an active internet connection with respect to effectiveness and speed.  Such comments such as "You're not testing for what you think you are!".

Considering my lack of success with Apple's Reachability Code, I have found that the asynchronous call to:

NSString *urlString = @"myURL";
NSURL *theURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];

which does hook up with the delegate method:
- (void )connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

This magic does indeed work, but I could fry a couple of eggs while I'm waiting and I know that the iApp folks will bounce me for that delay.

Maybe my method of "taking my connection" down may be in error -- I am simply pushing off the "standby" button on my cable modem -- I have also just unplugged the modem.

With respect to my attempts in using Apple's Reachability code:

Here are the two properties:

    Reachability *itsInternetReachability;
    BOOL         itsInternetActive;

This method is called when I start my app:

- (void )startObserveInternetConnection
{
    self.itsInternetReachability = [Reachability reachabilityForInternetConnection];

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self
                                       selector:@selector(pingInternetConnection:)
                                          name:kReachabilityChangedNotification
                                         object:internetReachability_];

    [internetReachability_ startNotifier];
}


- (void )pingInternetConnection:(NSNotification *)sentNotification
{
    if ([[sentNotification object] isKindOfClass:[Reachability class]])
    {
        NetworkStatus internetStatus = [self.itsInternetReachability currentReachabilityStatus];
        self.itsInternetActive = (internetStatus != NotReachable);
    }
    else
    {
        self.itsInternetActive = NO;
    }
}

Isn't the function of -startNotifier to enable the callback method to be continuously called within the main RunLoop?

Yet the only time I can get this callback method to be called is if I call NSNotificationCenter's -postNotificationName elsewhere in the code??

-[notificationCenter postNotificationName:kReachabilityChangedNotification object:internetReachability_];

Triple thanks …


John Love
Touch the Future! Teach!



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Checking for Internet Connection in an iApp
      • From: Jens Alfke <email@hidden>
    • Re: Checking for Internet Connection in an iApp
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: IBOutlet getter/setter pattern question
  • Next by Date: Re: Checking for Internet Connection in an iApp
  • Previous by thread: Re: IBOutlet getter/setter pattern question
  • Next by thread: Re: Checking for Internet Connection in an iApp
  • Index(es):
    • Date
    • Thread