2

私の iPhone アプリでは、apples Reachability クラスを使用してネットワークの可用性を確認しています。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 に答える