iOS開發 - 檢測網路狀態(WIFI、2G/3G/4G)

來源:互聯網
上載者:User

iOS開發 - 檢測網路狀態(WIFI、2G/3G/4G)
檢測網路狀態

在網路應用中,需要對使用者裝置的網路狀態進行即時監控,目的是
讓使用者瞭解自己的網路狀態,防止一些誤會(比如怪應用無能)
根據使用者的網路狀態進行智能處理,節省使用者流量,提高使用者體驗
WIFI\3G網路:自動下載高清圖片
低速網路:只下載縮圖
沒有網路:只顯示離線的快取資料

蘋果官方提供了一個叫Reachability的樣本程式,便於開發人員檢測網路狀態
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

Reachability的使用步驟
添加架構SystemConfiguration.framework

添加原始碼

包含標頭檔

#import "Reachability.h"
抽取工具類
常見用法// 是否WIFI+ (BOOL) IsEnableWIFI {    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL) IsEnable3G {    return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);}
網路監控
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];self.netReachability = [Reachability reachabilityForInternetConnection];[self.netReachability startNotifier];- (void)dealloc{    [self.netReachability stopNotifier];    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];}
檢測網路狀態執行個體

NetWorkTool工具類

#import @interface NetworkTool : NSObject/** *  是否WIFI */+ (BOOL)isEnableWIFI;/** *  是否3G */+ (BOOL)isEnable3G;@end
#import "NetWorkTool.h"#import "Reachability.h"@implementation NetworkTool// 是否WIFI+ (BOOL)isEnableWIFI {    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL)isEnable3G {    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}@end

實作類別

#import "ViewController.h"#import "Reachability.h"#import "NetworkTool.h"@interface ViewController ()@property (nonatomic, strong) Reachability *reachability;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 監聽網路狀態發生改變的通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];    // 獲得Reachability對象    self.reachability = [Reachability reachabilityForInternetConnection];    // 開始監控網路    [self.reachability startNotifier];}- (void)dealloc{    [self.reachability stopNotifier];    [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)networkStateChange{    NSLog(@"網路狀態改變了");    [self checkNetworkState];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self checkNetworkState];}
#pragma mark - App應用程式網路類型改變就會調用- (void)networkStateChange{    if ([NetWorkTool isEnableWIFI]) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您現在網路環境為wifi!開始暢享吧!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];        [alert show];    } else if ([NetWorkTool isEnable3G]) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您"  message:@"您現在網路環境為3G/4G網路!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];        [alert show];    } else {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示您" message:@"您當前沒有可連的網路或已經掉線!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];        [alert show];    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.