iOS 检测网络连接
2016-01-11 16:25
483 查看
最近遇到需要检测网络状况的情况,在YYkit和AF里面有相关代码,供查阅。
//
// YYReachability.h
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 15/2/6.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
typedef NS_ENUM(NSUInteger, YYReachabilityStatus) {
YYReachabilityStatusNone = 0,
///< Not Reachable
YYReachabilityStatusWWAN = 1,
///< Reachable via WWAN (2G/3G/4G)
YYReachabilityStatusWiFi = 2,
///< Reachable via WiFi
};
typedef NS_ENUM(NSUInteger, YYReachabilityWWANStatus) {
YYReachabilityWWANStatusNone = 0,
///< Not Reachable vis WWAN
YYReachabilityWWANStatus2G = 2,
///< Reachable via 2G (GPRS/EDGE) 10~100Kbps
YYReachabilityWWANStatus3G = 3,
///< Reachable via 3G (WCDMA/HSDPA/...) 1~10Mbps
YYReachabilityWWANStatus4G = 4,
///< Reachable via 4G (eHRPD/LTE) 100Mbps
};
/**
`YYReachability` can used to monitor the network status of an iOS device.
*/
@interface YYReachability :
NSObject
@property (nonatomic,
assign, readonly)
SCNetworkReachabilityFlags flags;
///< Current flags.
@property (nonatomic,
assign, readonly)
YYReachabilityStatus status;
///< Current status.
@property (nonatomic,
assign, readonly)
YYReachabilityWWANStatus wwanStatus
NS_AVAILABLE_IOS(7_0);
///< Current WWAN status.
@property (nonatomic,
assign, readonly,
getter=isReachable) BOOL reachable;
/// Notify block which will be called on main thread when network changed.
@property (nonatomic,
copy) void (^notifyBlock)(YYReachability *reachability);
+ (instancetype)reachability;
+ (instancetype)reachabilityForLocalWifi;
+ (instancetype)reachabilityWithHostname:(NSString *)hostname;
+ (instancetype)reachabilityWithAddress:(const
struct sockaddr_in *)hostAddress;
@end
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
};
NS_ASSUME_NONNULL_BEGIN
//
// YYReachability.h
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 15/2/6.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
typedef NS_ENUM(NSUInteger, YYReachabilityStatus) {
YYReachabilityStatusNone = 0,
///< Not Reachable
YYReachabilityStatusWWAN = 1,
///< Reachable via WWAN (2G/3G/4G)
YYReachabilityStatusWiFi = 2,
///< Reachable via WiFi
};
typedef NS_ENUM(NSUInteger, YYReachabilityWWANStatus) {
YYReachabilityWWANStatusNone = 0,
///< Not Reachable vis WWAN
YYReachabilityWWANStatus2G = 2,
///< Reachable via 2G (GPRS/EDGE) 10~100Kbps
YYReachabilityWWANStatus3G = 3,
///< Reachable via 3G (WCDMA/HSDPA/...) 1~10Mbps
YYReachabilityWWANStatus4G = 4,
///< Reachable via 4G (eHRPD/LTE) 100Mbps
};
/**
`YYReachability` can used to monitor the network status of an iOS device.
*/
@interface YYReachability :
NSObject
@property (nonatomic,
assign, readonly)
SCNetworkReachabilityFlags flags;
///< Current flags.
@property (nonatomic,
assign, readonly)
YYReachabilityStatus status;
///< Current status.
@property (nonatomic,
assign, readonly)
YYReachabilityWWANStatus wwanStatus
NS_AVAILABLE_IOS(7_0);
///< Current WWAN status.
@property (nonatomic,
assign, readonly,
getter=isReachable) BOOL reachable;
/// Notify block which will be called on main thread when network changed.
@property (nonatomic,
copy) void (^notifyBlock)(YYReachability *reachability);
+ (instancetype)reachability;
+ (instancetype)reachabilityForLocalWifi;
+ (instancetype)reachabilityWithHostname:(NSString *)hostname;
+ (instancetype)reachabilityWithAddress:(const
struct sockaddr_in *)hostAddress;
@end
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
};
NS_ASSUME_NONNULL_BEGIN
相关文章推荐
- [Python]网络爬虫(三):异常的处理和HTTP状态码的分类
- xml,json,http,socket的区别
- 由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法
- XMLHttpRequest对象实例
- TI 网络摄像 解决方案
- Android利用Fiddler进行网络数据抓包
- httpclient4.3.3详解一:RequestConfig类解析
- 神经网络 调节参数
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/
- TCP、UDP、IP 协议分析
- Java CookBook Learning Day5th--HTTP (by Tim O'Brien)
- python监控TCP连接数
- SDN网络中的路由规则(四)
- QT5使用QTcpSocket类注意事项
- Android网络编程之HttpClient&HttpGet&HttpPost
- Capacitated minimum spanning tree https://en.wikipedia.org/wiki/Capacitated_minimum_spanning_tree
- 程序员之网络安全系列(三):数据加密之对称加密算法
- 网络协议的初始化dev_add_pack
- 通过/proc/net/dev分析网络包量,流量,错包,丢包