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];
    // ...
}

 

本人轉自:易飛揚的博客

 


免責聲明!

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



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