2

在我的 iPhone 应用程序中,我使用苹果的可达性类来检查网络可用性。使用 Wifi 时它工作正常。但是当我使用移动网络(3G)的应用程序时,它不工作

我找不到解决方案。如果有人遇到同样的问题..请帮忙

编辑 - 这是我的可达性代码

AppDelegate.h

   #import <UIKit/UIKit.h>
   #import "Reachability.h"

   @class ViewController;

   @interface AppDelegate : UIResponder <UIApplicationDelegate>{
    Reachability* hostReach;
    Reachability* internetReach;
    Reachability* wifiReach;

    }
   @property (strong, nonatomic) UIWindow *window;
   @property (nonatomic,assign)BOOL networkAvailable;

  -(void)updateInterfaceWithReachability: (Reachability*) curReach;
  -(void)displayNetworkAvailability:(id)sender;

   @end

AppDelegate.m

 #import "AppDelegate.h"
 implementation AppDelegate

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//reachability
[[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeAlert |
  UIRemoteNotificationTypeBadge |
  UIRemoteNotificationTypeSound)];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

hostReach = [Reachability reachabilityWithHostName: @"www.google.com"] ;
[hostReach startNotifier];
[self updateInterfaceWithReachability: hostReach];

internetReach = [Reachability reachabilityForInternetConnection] ;
[internetReach startNotifier];
[self updateInterfaceWithReachability: internetReach];

wifiReach = [Reachability reachabilityForLocalWiFi] ;
[wifiReach startNotifier];
[self updateInterfaceWithReachability: wifiReach];




 -(void) updateInterfaceWithReachability: (Reachability*) curReach
  {

if(curReach == hostReach)
{

  }
if(curReach == internetReach)
{   NSLog(@"internet reach");
    //[self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
if(curReach == wifiReach)
{
    NSLog(@"wifi reach");
    //[self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
}
NetworkStatus hostStatus = [curReach currentReachabilityStatus];
switch (hostStatus)

 {
    case NotReachable:
    {
        NSLog(@"A gateway to the host server is down.");

        self.networkAvailable=NO;
        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"A gateway to the host server is working via WIFI.");
        self.networkAvailable=YES;
        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"A gateway to the host server is working via WWAN.");
        self.networkAvailable=YES;
        break;

    }
}
if (!self.networkAvailable ) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"NO"];


}else {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"YES"];




   }


 }

 - (void) reachabilityChanged: (NSNotification* )note{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];

    }

  - (void) displayNetworkAvailability:(id)sender{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldnot connect to server" message:@"Please check network connection!!"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];


   [[NSNotificationCenter defaultCenter] postNotificationName:@"NONETWORKFOUND"  object:@"NO"];



   }

 @end
4

1 回答 1

0

我不明白你是否需要区分蜂窝和 WiFi,但如果你只需要一个类来检查互联网连接,你可以在这里查看我的 ALData 类: https ://gist.github.com/andrealufino/4994901

于 2013-02-20T11:28:18.700 回答