- 最简便的集成方法当属pod: pod ‘RealReachability’。
- 手动集成:将RealReachability文件夹加入到工程即可。
- 依赖:Xcode5.0+,支持ARC, iOS6+.项目需要引入SystemConfiguration.framework.
//打开检测
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
1.[GLobalRealReachability startNotifier];
return YES;
}
//在具体的页面添加观察者(实时监测网络的变化)
- (void)viewDidLoad
{
[super viewDidLoad];
2.[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
//当前网络连接状态 (单例调用简单)
3.ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
NSLog(@"Initial reachability status:%@",@(status));
if (status == RealStatusNotReachable)
{
self.flagLabel.text = @"Network unreachable!";
}
if (status == RealStatusViaWiFi)
{
self.flagLabel.text = @"Network wifi! Free!";
}
if (status == RealStatusViaWWAN)
{
self.flagLabel.text = @"Network WWAN! In charge!";
}
}
//观察者实时检测方法
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
if (status == RealStatusNotReachable)
{
self.flagLabel.text = @"Network unreachable!";
}
if (status == RealStatusViaWiFi)
{
self.flagLabel.text = @"Network wifi! Free!";
}
if (status == RealStatusViaWWAN)
{
self.flagLabel.text = @"Network WWAN! In charge!";
}
WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
if (status == RealStatusViaWWAN)
{
if (accessType == WWANType2G)
{
self.flagLabel.text = @"RealReachabilityStatus2G";
}
else if (accessType == WWANType3G)
{
self.flagLabel.text = @"RealReachabilityStatus3G";
}
else if (accessType == WWANType4G)
{
self.flagLabel.text = @"RealReachabilityStatus4G";
}
else
{
self.flagLabel.text = @"Unknown RealReachability WWAN Status, might be iOS6";
}
}
}
iOS下的实际网络连接检测:RealReachability
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 网络连接状态检测对于我们的iOS app开发来说是一个非常通用的需求。为了更好的用户体验,我们会在无网络时展现本地...