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

 

PRJ_reachability.zip 例子下載地址:http://ishare.iask.sina.com.cn/f/37441462.html

百度雲盤分享鏈接:http://pan.baidu.com/s/1o6qhQCa

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM