How can I check if an internet connection is valid in iOS?

Discussion in 'Programming General' started by kmjt, Oct 15, 2014.

How can I check if an internet connection is valid in iOS?
  1. Unread #1 - Oct 15, 2014 at 12:35 PM
  2. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    How can I check if an internet connection is valid in iOS?

    I implemented the Reachability class but to my knowledge that only detects if the internet is "reachable". Here is the class:
    https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

    I want to know before someone clicks on a link (for example) in the app if the internet is working. By working I mean it will actually load. Not if it is just reachable.

    With the Reachability class, if I turn off wifi on my phone, the following method will still return true:

    Code:
    -(bool)canReachInternet
    {
        Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
        NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
        
        if (networkStatus == NotReachable)
            return false;
        
        else
            return true;
    }
    
    This is because under Cellular settings I have "Enable 3G" set to on. However my phone plan cannot get internet unless on wifi. When "Enable 3G" is set to on, the above method turns true (because technically WWAN is reachable). When it is turned off (as well as wifi turned off), the method will return false. However I can't expect an app user to know to turn off "Enable 3G" so this method is pretty useless for determining if they can actually connect to the internet or not.




    I have also tried the following method:

    Code:
    - (BOOL)isNetworkAvailable
    {
        char *hostname;
        struct hostent *hostinfo;
        hostname = "microsoft.com";
        hostinfo = gethostbyname(hostname);
        
        if (hostinfo == NULL)
            return false;
        
        else
            return YES;
    }
    
    However I don't think it works all that well because sometimes if a website is cached it will return true even when there is no internet connection.



    The bottom line is how can I detect if internet is working on a phone? Not just if it is reachable (which is what the Reachability class determines).
     
  3. Unread #2 - Oct 15, 2014 at 6:11 PM
  4. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    How can I check if an internet connection is valid in iOS?

    I've never had any complaints using that example in a live app. And if it were to return true it would crash the app resulting in a crash report.

    Where did you gather this information? ( I may need to look into reachability too ).

    Also, I haven't tried this. But

    Code:
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    Could that not help?
     
  5. Unread #3 - Oct 15, 2014 at 8:07 PM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    How can I check if an internet connection is valid in iOS?




    I'll test it out and let you know.
     
  7. Unread #4 - Oct 16, 2014 at 2:58 PM
  8. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    How can I check if an internet connection is valid in iOS?

    I tested it and it appears to work exactly like the Reachability class. It will also return true if I have 3G enabled on my phone (but my plan will not actually let me access internet). Is there anyway I can detect if the internet will actually work? Not just if it is reachable?
     
  9. Unread #5 - Oct 16, 2014 at 6:10 PM
  10. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    How can I check if an internet connection is valid in iOS?

    It only makes sense that it would return true if it receives a response from microsoft.com thus making it irrelevant of what network status you are currently holding as it would only return true if you could receive data from the source. <- that was my belief.

    I also cannot help too much as my phone has active 3g.. But I'll have a little think and let you know if I come up with anything.

    May not be the best way of doing it, or may not work.. As I can't test for your issue..
    Code:
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.microsoft.com/"]
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
    receivedData = [NSMutableData dataWithCapacity: 0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
        //Connection Not Established
        receivedData = nil;
    
    } else {
        //Connection Established
    }
    
     
  11. Unread #6 - Oct 29, 2014 at 3:47 PM
  12. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    How can I check if an internet connection is valid in iOS?

    I don't know obj-C cause it's a shit language, but it's pretty standard to test connectivity with one of the following two methods:
    - ping a reliable site (faster)
    - webrequest a reliable site and check if html was received (if htmlstring != null then signal received) (slow)
     
  13. Unread #7 - Oct 31, 2014 at 7:40 PM
  14. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    How can I check if an internet connection is valid in iOS?

< wifi password | Looking for an idea to get myself the C# Programmer rank....ideas!? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site