1. 程式人生 > >ios檢測網路狀態改變

ios檢測網路狀態改變

使用之前請從Apple網站下載示例:點此下載

然後將Reachability.h 和 Reachability.m 加到自己的專案中,並引用 SystemConfiguration.framework,就可以使用了。

Reachability 中定義了3種網路狀態:

複製程式碼
// the network state of the device for Reachability 1.5.
typedef enum {
    NotReachable = 0,  //無連線
    ReachableViaCarrierDataNetwork, //使用3G/GPRS網路
    ReachableViaWiFiNetwork  //
使用WiFi網路 } NetworkStatus; // the network state of the device for Reachability 2.0. typedef enum { NotReachable = 0, //無連線 ReachableViaWiFi, //使用3G/GPRS網路 ReachableViaWWAN //使用WiFi網路 } NetworkStatus;
複製程式碼

比如檢測某一特定站點的接續狀況,可以使用下面的程式碼:

複製程式碼
Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];
switch ([r currentReachabilityStatus]) { case NotReachable: // 沒有網路連線 break; case ReachableViaWWAN: // 使用3G網路 break; case ReachableViaWiFi: // 使用WiFi網路 break; }
複製程式碼

檢測當前網路環境:

複製程式碼
// 是否wifi
+ (BOOL) IsEnableWIFI {
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

// 是否3G + (BOOL) IsEnable3G { return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable); }
複製程式碼

連線狀態實時通知

網路連線狀態的實時檢查,通知在網路應用中也是十分必要的。接續狀態發生變化時,需要及時地通知使用者。由於Reachability1.5版與2.0版有一些變化,這裡分開來說明使用方法。

Reachability 1.5

複製程式碼
// My.AppDelegate.h

#import "Reachability.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    NetworkStatus remoteHostStatus;
}

@property NetworkStatus remoteHostStatus;

@end

// My.AppDelegate.m

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize remoteHostStatus;

// 更新網路狀態
- (void)updateStatus {
    self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}

// 通知網路狀態
- (void)reachabilityChanged:(NSNotification *)note {
    [self updateStatus];
    if (self.remoteHostStatus == NotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

// 程式啟動器,啟動網路監視
- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // 設定網路檢測的站點
    [[Reachability sharedReachability] setHostName:@"www.apple.com"];
    [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
    // 設定網路狀態變化時的通知函式
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
                                                 name:@"kNetworkReachabilityChangedNotification" object:nil];
    [self updateStatus];
}

- (void)dealloc {
    // 刪除通知物件
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [window release];
    [super dealloc];
}
複製程式碼

Reachability 2.0

複製程式碼
// MyAppDelegate.h

@class Reachability;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    Reachability  *hostReach;
}

@end

// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    NetworkStatus status = [curReach currentReachabilityStatus];

    if (status == NotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
                                                        message:@"NotReachable"
                                                       delegate:nil
                                              cancelButtonTitle:@"YES" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // ...

    // 監測網路情況
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(reachabilityChanged:)
                                          name: kReachabilityChangedNotification
                                          object: nil];
    hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
    [hostReach startNotifer];
    // ...
}
複製程式碼