網絡鏈接

然後將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];
    // ...
}
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章