在我的iPhone应用程序中,我使用apple 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
答案 0 :(得分:0)
我不明白您是否需要区分移动电话和WiFi,但如果您只需要一个班级来检查互联网连接,您可以在此处查看我的ALData课程: https://gist.github.com/andrealufino/4994901