简体   繁体   中英

Always getting Not Reachable in iOS7

Earlier i have always used Tony Million's Reach-ability and it always worked fine.Now i have also tried Apple's Reach-ability but each time i try to access i always get Internet not Reachable response.

Is there anything else i should check?

Here is what i did

  Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];

    reach.reachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            blockLabel.text = @"Block Says Reachable";
        });
    };

    reach.unreachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            blockLabel.text = @"Block Says Unreachable";
        });
    };

    [reach startNotifier];

You can also use for checking Internet connection, First you need to import in you file

#import <SystemConfiguration/SCNetworkReachability.h>
#include <netinet/in.h>

in .h file

- (BOOL) connectedToNetwork
{

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

    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags)
    {
        return NO;
    }

    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable && !needsConnection) ? YES : NO;
}

If this methods return YES then internet connection is available.

May this help you...

if face same problem so i open Reachability.m file and commit 3g.

#if TARGET_OS_IPHONE
    if(flags & kSCNetworkReachabilityFlagsIsWWAN)
    {
       // We're on 3G.
       if(!self.reachableOnWWAN)
          {
          // We don't want to connect when on 3G.
           connectionUP = NO;//Commit this line
         }
    }
#endif

Hope this will solve your problem.

#if TARGET_OS_IPHONE
    if(flags & kSCNetworkReachabilityFlagsIsWWAN)
    {
       // We're on 3G.
       if(!self.reachableOnWWAN)
          {
          // We don't want to connect when on 3G.
           //connectionUP = NO;
         }
    }
#endif
    Reachability *r = [Reachability reachabilityWithHostName:@"www.Google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        blockLabel.text = @"Block Says Unreachable";
    }
    else
    {
        blockLabel.text = @"Block Says Reachable";
    }

The old Reachability files are no good. Apple has updated their reachability files.

Check here.

https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html

Download here.

https://developer.apple.com/Library/ios/samplecode/Reachability/Reachability.zip

I have a same issue with the Tony Million's Reachability : network status was always set to NotReachable. I fix it with adding the SystemConfiguration Framework

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM