0

I am a new iPhone developer. I have created an iOS application where I have checked internet availability checking.

I have used the following method:

-(BOOL)connectedToInternet
{
   NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]
                                                  encoding:NSStringEncoding
                                                     error:nil];
   return ( URLString != NULL ) ? YES : NO;
}

In the applicationDidFinishLaunching I have called the method and show warning if no connection otherwise enter the second screen.

I have downloaded the app in my iPhone (iOS 4.3.1) and iPad (iOS 4.3.3). It successfully works.

But my client said that he download on his iPhone (iOS 4.3.5) and it always shows "No Internet Connection" warning.

I have searched several site to find the problem and a permanent solution. But I did not find what is my fault and why app not working all device.

iHunter
  • 6,205
  • 3
  • 38
  • 56
Harun Sagar
  • 63
  • 1
  • 8

4 Answers4

4

First of all, this is a bad way to check for connectivity. Look at Apple's Reachability sample code instead.

Second, you did this:

NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] encoding:NSStringEncoding   error:nil];

Stop passing nil as the error parameter.

NSError *error;
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] encoding:NSStringEncoding error:&error];

If URLString comes back nil, put the error in your alert.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

Well personally I use the following technique to test Internet availability (whether WiFi or 3G). I add a special class for that (in my case I call it WiFiCheckClass), and in the .h file I do:

#import <Foundation/Foundation.h>
#import "SystemConfiguration/SCNetworkReachability.h"

@interface UIDevice (DeviceConnectivity)
+(BOOL) cellularConnected;
+(BOOL) wiFiConnected;
+(BOOL) networkConnected;
@end

And in the .m file I do:

#import "WiFiCheckClass.h"

@implementation UIDevice (DeviceConnectivity)

+(BOOL) cellularConnected
{
    SCNetworkReachabilityFlags  flags = 0;
    SCNetworkReachabilityRef netReachability;
    netReachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"www.google.com" UTF8String]);
    if(netReachability)
    {
        SCNetworkReachabilityGetFlags(netReachability, &flags);
        CFRelease(netReachability);
    }
    if(flags & kSCNetworkReachabilityFlagsIsWWAN) return YES;
    return NO;
}

+(BOOL) networkConnected
{
    SCNetworkReachabilityFlags flags = 0;
    SCNetworkReachabilityRef netReachability;
    BOOL  retrievedFlags = NO;
    netReachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"www.google.com" UTF8String]);
    if(netReachability)
    {
        retrievedFlags  = SCNetworkReachabilityGetFlags(netReachability, &flags);
        CFRelease(netReachability);
    }
    if (!retrievedFlags || !flags) return NO;
    return YES;
}

+(BOOL) wiFiConnected
{
    if ([self cellularConnected]) return NO;
    return [self networkConnected];
}

@end

Now any time I need to know if there is Internet connection I import the library and do the following test:

if([UIDevice cellularConnected] || [UIDevice networkConnected] || [UIDevice wiFiConnected])
{
    //there is Internet, do what you want
}
antf
  • 3,162
  • 2
  • 26
  • 33
0

You probably want to change that method to the "correct" way of detecting internet availability on IOS using the reachability APIs.

Have a look at for example this SO answer or Apples sample how to do it.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

The apple recommended way to see if a device is online is to use the Reachability class - see the sample code here : http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

deanWombourne
  • 38,189
  • 13
  • 98
  • 110