IOS Reachability判断所请求服务器是否超时?(转)

开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。

Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。

1.在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。

2.然后将 SystemConfiguration.framework 添加进工程。

我使用的版本为 : Version: 2.2

我为Apple的例程增加了一个全局 -- ReachabilityAutoChecker

.h

复制代码
@interface ReachabilityAutoChecker : NSObject

@property (nonatomic, retain) Reachability  *reachability;
@property (nonatomic, assign) NetworkStatus networkStatus;
@property (nonatomic, assign) BOOL          connectionRequired;

@end
复制代码

.m文件

复制代码
@implementation ReachabilityAutoChecker

@synthesize reachability;

+ (id)sharedChecker
{
    static ReachabilityAutoChecker *staticChecker = nil;
    if (!staticChecker) {
        staticChecker = [[ReachabilityAutoChecker alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:staticChecker selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        staticChecker.networkStatus = NotReachable;
        staticChecker.connectionRequired = NO;
    }
    return staticChecker;
}

- (void)reachabilityChanged:(NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    self.networkStatus = [curReach currentReachabilityStatus];
    self.connectionRequired = [curReach connectionRequired];
}

@end
复制代码

我为Apple的例程增加了一个Category -- Reachability (AutoChecker)

.h文件:

@interface Reachability (AutoChecker)

+ (void)startCheckWithReachability:(Reachability *)reachability;
+ (BOOL)isReachable;

@end

.m文件:

复制代码
@implementation Reachability (AutoChecker)

+ (void)startCheckWithReachability:(Reachability *)reachability
{
    ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker];
    
    if (checker.reachability) {
        [checker.reachability stopNotifier];
        checker.reachability = nil;
    }
    
    checker.reachability = reachability;
    [checker.reachability startNotifier];
}

+ (BOOL)isReachable
{
    ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker];
    
    if (!checker.reachability) {
        NSLog(@"Check Reachability With No Reachability has been Set!");
        return NO;
    }
    
    NetworkStatus networkStatus = [checker networkStatus];
    
    if(networkStatus == ReachableViaWiFi)
    {
        NSLog(@"WIFI");
    }
    if(networkStatus == ReachableViaWWAN)
    {
        NSLog(@"3G");
    }
    
    BOOL connectionRequired = NO;
    connectionRequired = [checker connectionRequired];
    
#if kShouldPrintReachabilityFlags
    NSLog(@"NetworkStatus %d connectionRequired %d", networkStatus, connectionRequired);
#endif
    
    if (networkStatus)
        return YES;
    else
        return NO;
}

@end
复制代码

调用方式:

AppDelegate.m

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //检测某一特定站点的接续状况,可以使用下面的代码
    Reachability *pReachability = [Reachability reachabilityWithHostName:@"appservices.comcsoft.com"];
    //开始监控网络状态
    [Reachability startCheckWithReachability:pReachability];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
复制代码

ViewController.m

复制代码
- (IBAction)upInside_checkNetStatus:(id)sender
{
    if(![Reachability isReachable])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"对不起,网络异常,请检查您的网络设置。"
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
}
复制代码

运行时提醒下 :  #error "此类需要在非arc环境下编译,请添加-fno-objc-arc标记"

网络正常 NSLog如下:

2013-07-05 14:15:53.084 PRJ_reachability[8153:11303] Reachability Flag Status: -R ------- networkStatusForFlags

2013-07-05 14:15:54.265 PRJ_reachability[8153:11303] WIFI

2013-07-05 14:15:54.266 PRJ_reachability[8153:11303] NetworkStatus 1 connectionRequired 0

原文地址:https://www.cnblogs.com/pjl111/p/4230744.html