Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:45:47+00:00 2026-06-11T23:45:47+00:00

This is the code I use with Reachability in my app: – (void) handleNetworkChange:(NSNotification

  • 0

This is the code I use with Reachability in my app:

- (void) handleNetworkChange:(NSNotification *)notice
{

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        UIAlertView *alertnew = [[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message:@"Your device lost internet connection!"
                                                       delegate:self
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:@"Dismiss", nil];
        [alertnew show];
    } else if (remoteHostStatus == ReachableViaWWAN) {
        //if connected to the Network
        UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                                 otherButtonTitles:@"Ok!", nil];
        [alertre show];
    } else if (remoteHostStatus == ReachableViaWiFi) {
        //if connected to Wi-Fi
        UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                                otherButtonTitles:@"Ok!", nil];
        [alertre show];
    }

}

I want to alert the users when they lose internet and when they gain it back. But the problem I’m running into is when a user is on an iPhone (with constant network connection) and they connect to Wi-Fi, they get an alert, when they were already connected. So I would only want to alert them if they DIDN’T have an internet connection and connected to Wi-Fi. This code works great for iPod and iPads without data plans, but the problem is on iPhones. Is there something I can do to edit this and make it only show an alert for one or the other?

I tried this code for the last alert:

else if (remoteHostStatus == ReachableViaWiFi && !(remoteHostStatus == ReachableViaWWAN)) {
        //if connected to Wi-Fi but without Network
        UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                                otherButtonTitles:@"Ok!", nil];
        [alertre show];
    }

But this didn’t fix the problem…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:45:48+00:00Added an answer on June 11, 2026 at 11:45 pm

    The basic error in logic of your code is that you are treating three separate states as two states. NetworkStatus in Apple’s Reachability example is defined by an enum seen here:

    typedef enum {
        NotReachable = 0,
        ReachableViaWiFi,
        ReachableViaWWAN
    } NetworkStatus;
    

    Since you essentially want a BOOL (Reachable or Not). Simply compare the current network status to NotReachable. So that in the case of either ReachableViaWiFi or ReachableViaWWAN the BOOL is YES.

    BOOL reachable = (_reachability.currentReachabilityStatus != NotReachable); // underbar added for good ivar style.
    

    Now you just have to handle the case where you had WiFi and moved to cellular or vise-versa. For this case you will have to track the state between calls. Add a BOOL ivar named _reachable. Then compare your new value in your observer method.

    -(void)reachabilityDidChange:(NSNotification *)note{
        BOOL reachable = (_reachability.currentReachabilityStatus != NotReachable);
        if (reachable != _reachable){
            // State Changed. Inform the user if necessary.
            if (reachable){
                // Connected.
            } else {
                // Lost connection.
            }
        }
        _reachable = reachable;
    }
    

    warning: unsolicited advise below

    One last note about the idea of these UIAlertViews in general. Alerts are essentially screaming at the user. An alert on reconnection is essentially yelling “HEY!!! EVERYTHING IS OKAY NOW!”. I would like to recommend watching session 221 of WWDC2012 where they speak of common design pitfalls. At about 35 minutes in they begin speaking of alerts. It’s clear that they are trying to steer developers away from using alerts as much as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make call from my app with this code (i use webview because after
I know this code use to work, I must have accidentally changed something when
hello I have this code that use to sort a datatable. Dim sortingIndex As
How could I refactor this code to use only one Dir[ ] call? Dir[
Here is the code I use to define my array, this code is above
Hi I use this code to save an xml file using (IsolatedStorageFile myIsolatedStorage =
If I use this code outside of a method or winmain: ostringstream vertexid; vertexid
I need use this code: <%= button_tag :class => btn btn-primary do %> <%=
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
When i use this code <!--[if IE 6]> <link rel=stylesheet href=ie6.css type=text/css /> <![endif]-->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.