• Log in or sign up for an account here: https://www.sythe.org/login

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

Joined
Aug 21, 2009
Sythe Gold
0
Messages
14,450
Location
Canada
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).
 
Gohan has AIDS
Sythe's 10th Anniversary
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.

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?
 
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?




I'll test it out and let you know.
 
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?
 
Gohan has AIDS
Sythe's 10th Anniversary
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
}
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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)
 
duelarena.gg
Back
Top