[iOS 多线程 & 网络 - 2.8] - 检测网络状态


A、说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:
(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
  WIFI\3G网络:自动下载高清图片
  低速网络:只下载缩略图   没有网络:只显示离线的缓存数据 苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 注意:此功能只能用真机测试,iOS模拟器,一直都有wifi信号,即使关掉或者使用飞行模式. B、监测网络状态 Reachability的使用步骤 添加框架SystemConfiguration.framework 包含头文件 #import "Reachability.h"
 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 2     // 检测wifi状态
 3     Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi];
 4    
 5     // 检测手机网络状态
 6     Reachability *netStatus = [Reachability reachabilityForInternetConnection];
 7    
 8     // 判断网络状态
 9     if ([wifiStatus currentReachabilityStatus] != NotReachable) {
10         NSLog(@"正在使用wifi上网");
11     } else if ([netStatus currentReachabilityStatus] != NotReachable) {
12         NSLog(@"正在使用手机网络上网");
13     } else {
14         NSLog(@"没有网络");
15     }  
16 }
17  
C.实时监测网络状态 使用通知监控 网络状态类要发送通知给控制器 销毁控制器的时候一定要删除通知、停止发送消息
 1 //
 2 //  ViewController.m
 3 //  ReachabilityDemo
 4 //
 5 //  Created by hellovoidworld on 15/1/28.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "Reachability.h"
11 
12 @interface ViewController ()
13 
14 @property(nonatomic, strong) Reachability *networkStatus;
15 
16 @end
17 
18 @implementation ViewController
19 
20 - (void)viewDidLoad {
21     [super viewDidLoad];
22     // Do any additional setup after loading the view, typically from a nib.
23    
24     // 实时监控手机联网状态
25     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectNetworkStatus) name:kReachabilityChangedNotification object:nil];
26    
27     // 开启通知发送
28     self.networkStatus = [Reachability reachabilityForInternetConnection];
29     [self.networkStatus startNotifier];
30 }
31 
32 - (void)dealloc {
33     // 停止发送通知
34     [self.networkStatus stopNotifier];
35    
36     // 切记要删除通知
37     [[NSNotificationCenter defaultCenter] removeObserver:self];
38 }
39 
40 // 用WIFI
41 // [wifi currentReachabilityStatus] != NotReachable
42 // [conn currentReachabilityStatus] != NotReachable
43 
44 // 没有用WIFI, 只用了手机网络
45 // [wifi currentReachabilityStatus] == NotReachable
46 // [conn currentReachabilityStatus] != NotReachable
47 
48 // 没有网络
49 // [wifi currentReachabilityStatus] == NotReachable
50 // [conn currentReachabilityStatus] == NotReachable
51 
52 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
53     [self detectNetworkStatus];
54 }
55 
56 - (void) detectNetworkStatus {
57     // 检测wifi状态
58     Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi];
59    
60     // 检测手机网络状态
61     Reachability *netStatus = [Reachability reachabilityForInternetConnection];
62    
63     // 判断网络状态
64     if ([wifiStatus currentReachabilityStatus] != NotReachable) {
65         NSLog(@"正在使用wifi上网");
66     } else if ([netStatus currentReachabilityStatus] != NotReachable) {
67         NSLog(@"正在使用手机网络上网");
68     } else {
69         NSLog(@"没有网络");
70     }
71 }
72 
73 @end
74  

优质内容筛选与推荐>>
1、请教C#.net从一个窗体引用另一个窗体控件的问题。
2、spring quartz使用多线程并发“陷阱”
3、.net2.0成功在于总结+行动
4、第九周周记
5、.NET设计模式(2):单件模式(Singleton Pattern)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn