0

everybody.

Do you think the code below is appropriate to check the internet connection in iOS? If I keep it, could i have any kind of problem? Is it too weak? Until now, it is working for me without problem. What do you think? Thank you.

    stringToURL = [NSString [NSString stringWithFormat: @"http://www.mycompany.com/File.csv"];
    url = [NSURL URLWithString:stringToURL];
    NSError *error = nil;
    content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    if (error != nil) {
            //Do something
    } else {
            //Keep running the app
    }
George
  • 19
  • 1
  • As for when to retry or wait for a reachability notification: http://stackoverflow.com/a/15854823/412916 – Jano Jun 12 '13 at 01:33

2 Answers2

1

Use the following code to check internet connectivity,

In .h

#import <Foundation/Foundation.h>

@interface NetworkConnectivity : NSObject

+ (BOOL)hasConnectivity;

@end

In .m

#import "NetworkConnectivity.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>

@implementation NetworkConnectivity


+ (BOOL)hasConnectivity {

    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);

    if(reachability != NULL) {

        SCNetworkReachabilityFlags flags;

        if (SCNetworkReachabilityGetFlags(reachability, &flags)) {

            if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
            {
                // if target host is not reachable
                return NO;
            }

            if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
            {
                // if target host is reachable and no connection is required
                //  then we'll assume (for now) that your on Wi-Fi
                return YES;
            }


            if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
                 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
            {
                // ... and the connection is on-demand (or on-traffic) if the
                //     calling application is using the CFSocketStream or higher APIs

                if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
                {
                    // ... and no [user] intervention is needed
                    return YES;
                }
            }

            if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
            {
                // ... but WWAN connections are OK if the calling application
                //     is using the CFNetwork (CFSocketStream?) APIs.
                return YES;
            }
        }
    }

    return NO;
}


@end

Then check where ever you want like,

if ([NetworkConnectivity hasConnectivity]) {

    // Internet available

} else {

    // Internet not available

}
Venk
  • 5,949
  • 9
  • 41
  • 52
0

Add Tony Million's version of Reachability.h and Reachability.m to the project Found here: https://github.com/tonymillion/Reachability

Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35