国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS中使用Reachability檢測網絡

2019-11-14 18:10:26
字體:
來源:轉載
供稿:網友

iOS中使用 Reachability 檢測網絡

內容提示:下提供離線模式(Evernote)。那么你會使用到Reachability來實現網絡檢測。   寫本文的目的 了解Reachability都能做什么 檢測3中網絡環境 2G/3G wifi 無網絡 如何使用通知 單個controller 多個controller 簡單的功能: 僅在wifi下使用 Reachability簡介 Reachablity 是一個iOS下...

 

如果你想在iOS程序中提供一僅在wifi網絡下使用(Reeder),或者在沒有網絡狀態下提供離線模式(Evernote)。那么你會使用到Reachability來實現網絡檢測。

 

寫本文的目的

  • 了解Reachability都能做什么
  • 檢測3中網絡環境
    • 2G/3G
    • wifi
    • 無網絡
  • 如何使用通知
    • 單個controller
    • 多個controller
  • 簡單的功能:
    • 僅在wifi下使用

Reachability簡介

Reachablity 是一個iOS下檢測,iOS設備網絡環境用的庫。

  • 監視目標網絡是否可用
  • 監視當前網絡的連接方式
  • 監測連接方式的變更

蘋果官方提供的Doc

http://developer.apple.com/library/ios/

 

Github上的文檔

https://github.com/tonymillion/Reachability

 

安裝

  1. 創建 network 工程(network是我創建的demo工程,附件中可以下載到)
  2.  使用Cocoaspod安裝依賴
  3. 在項目中添加 SystemConfiguration.framework 庫

由于Reachability非常常用。直接將其加入到Supporting Files/networ-PRefix.pch中:

 

1
#import <Reachability/Reachability.h>

 

 如果你還不知道cocoaspod是什么,看這里:

http://witcheryne.VEvb.com/blog/1873221

 

使用

stackoverflow上有一篇回答,很好的解釋了reachability的用法

http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability

  • 一般情況一個Reachability實例就ok了。
  • 一個Controller只需要一個Reachability

Block方式使用

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- (void)viewDidLoad
{
    [super viewDidLoad];
     DLog(@"開啟 www.apple.com 的網絡檢測");
     Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
     DLog(@"-- current status: %@", reach.currentReachabilityString);
     
     // start the notifier which will cause the reachability object to retain itself!
     
     [[NSNotificationCenter defaultCenter] addObserver:self
                                                        selector:@selector(reachabilityChanged:)
                                                             name:kReachabilityChangedNotification
                                                          object:nil];
     
     reach.reachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.blockLabel.text = @"網絡可用";
               self.blockLabel.backgroundColor = [UIColor greenColor];
        });
    };
    
    reach.unreachableBlock = ^(Reachability * reachability)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.blockLabel.text = @"網絡不可用";
               self.blockLabel.backgroundColor = [UIColor redColor];
        });
    };
     
     [reach startNotifier];
}

 

 

使用notification的方式

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
- (void)viewDidLoad
{
    [super viewDidLoad];
     DLog(@"開啟 www.apple.com 的網絡檢測");
     Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
     DLog(@"-- current status: %@", reach.currentReachabilityString);
     
     // start the notifier which will cause the reachability object to retain itself!
     
     [[NSNotificationCenter defaultCenter] addObserver:self
                                                        selector:@selector(reachabilityChanged:)
                                                             name:kReachabilityChangedNotification
                                                          object:nil];
     [reach startNotifier];
}
 
- (void) reachabilityChanged: (NSNotification*)note {
     Reachability * reach = [note object];
    
    if(![reach isReachable])
    {
        self.notificationLabel.text = @"網絡不可用";
          self.notificationLabel.backgroundColor = [UIColor redColor];
          self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
          self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
          return;
    }
        
     self.notificationLabel.text = @"網絡可用";
     self.notificationLabel.backgroundColor = [UIColor greenColor];
     
     if (reach.isReachableViaWiFi) {
          self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];
          self.wifiOnlyLabel.text = @"當前通過wifi連接";
     } else {
          self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
          self.wifiOnlyLabel.text = @"wifi未開啟,不能用";
     }
     
     if (reach.isReachableViaWWAN) {
          self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];
          self.wwanOnlyLabel.text = @"當前通過2g or 3g連接";
     } else {
          self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
          self.wwanOnlyLabel.text = @"2g or 3g網絡未使用";
     }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂宁市| 华容县| 行唐县| 疏附县| 思茅市| 方正县| 河曲县| 紫阳县| 孝感市| 桓仁| 张北县| 皮山县| 万载县| 油尖旺区| 淳安县| 奉新县| 乌拉特中旗| 乌恰县| 隆回县| 辰溪县| 崇仁县| 疏附县| 达拉特旗| 屏东县| 罗平县| 岳西县| 旌德县| 包头市| 新余市| 习水县| 苗栗县| 墨竹工卡县| 牟定县| 尉犁县| 青州市| 同德县| 雷州市| 阿荣旗| 大洼县| 泊头市| 龙井市|