判断网络环境(3G/WIFI)
来源:互联网 发布:网络举报应用账号格式 编辑:程序博客网 时间:2024/05/03 02:14
通过Reachability来判断设备的网络环境,方法比较简单。直接将Reachability.h和Reachability.m加入到工程中,然后添加SystemConfiguration.framework框架,就可以使用了。工程截图:
ViewController.h
//// ViewController.h// NetworkStatusDemo//// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import <UIKit/UIKit.h>#import "Reachability.h"@interface ViewController : UIViewController{ IBOutlet UILabel *netstatus; Reachability* status; //网络状态}@property (retain, nonatomic) IBOutlet UILabel *netstatus;@property (retain, nonatomic) Reachability* status;+ (BOOL) IsEnableWIFI;+ (BOOL) IsEnable3G;@end
ViewController.m
//// ViewController.m// NetworkStatusDemo//// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import "ViewController.h"#import "Reachability.h"@implementation ViewController@synthesize netstatus;@synthesize status;- (void)viewDidLoad{ [super viewDidLoad]; self.status = [[Reachability alloc] init]; status = [Reachability reachabilityWithHostName:@"www.cnblogs.com/foxmin"]; switch ([status currentReachabilityStatus]) { case NotReachable: // 没有网络连接 self.netstatus.text = @"没有网络连接"; break; case ReachableViaWWAN: // 使用3G网络 self.netstatus.text = @"使用3G网络"; break; case ReachableViaWiFi: // 使用WiFi网络 self.netstatus.text = @"使用WiFi网络"; break; } //程序启动时,检查程序的网络环境 if ([ViewController IsEnableWIFI] && ![ViewController IsEnable3G]) { self.netstatus.text = @"使用WiFi网络"; }else if(![ViewController IsEnableWIFI] && [ViewController IsEnable3G]){ self.netstatus.text = @"使用3G网络"; }else self.netstatus.text = @"没有网络连接"; }- (void)viewDidUnload{ [self setNetstatus:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}- (void)dealloc { [netstatus release]; [super dealloc];}/* *判断是否通过wifi */+ (BOOL) IsEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}/* *判断是否通过3G */+ (BOOL) IsEnable3G { return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}@end
0 0
- 判断网络环境(3G/WIFI)
- iPhone开发—判断网络环境3G/WIFI
- 判断当前网络类型(WIFI,2G,3G)
- Reachability.h 开源类的使用:判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
- Android杂谈--网络状态判断(3G/WIFI)
- android网络判断(wifi、3G)
- 确认网络环境 3G/WIFI
- 确认网络环境3G/WIFI
- 确认网络环境 3G/WIFI
- WIFI、3G、2G网络连接判断深入分析
- Android网络类型判断(2g、3g、wifi)
- android网络状态判断(wifi、3G、2G)
- Android网络类型判断(2g、3g、wifi) .
- Android网络类型判断(2g、3g、wifi)
- iOS平台判断2G,3G,wifi网络类型
- android网络状态判断(wifi、3G、2G)
- Android网络类型判断(2g、3g、wifi)
- Android网络类型判断(2g、3g、wifi)
- MyISAM 和 InnoDB 讲解(转)
- myclipse 莫名其妙的编译不过去,最终把编码方式删除了才解决
- 手动move region做load balance
- 第二周(项目三)——文件+结构体实现实用系统。<总是不对,求大神指点迷津!!>
- 2013蓝桥杯【模拟赛】硬币方案
- 判断网络环境(3G/WIFI)
- 性能测试学习中的问题与解答6
- JS继承三种方式
- masm32开发com组件介绍[一][二][三]
- RMAN学习笔记
- 算法基础(七):二叉排序树基本操作-插入、删除(附源代码加注释)
- 简单理解zookeeper
- 批处理精练版
- git基本使用