iPhone开发 判断当前的网络是3g还是wifi
来源:互联网 发布:java架构师是什么意思 编辑:程序博客网 时间:2024/05/17 04:44
1.添加framework:
将SystemConfiguration.framework 添加进工程。
2.下载Reachability
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
复制里面的Reachability.h和Reachability.m到项目中。
3.调用代码:
//判断当前的网络是3g还是wifi-(NSString*)GetCurrntNet{ NSString* result; Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"]; switch ([r currentReachabilityStatus]) { case NotReachable:// 没有网络连接 result=nil; break; case ReachableViaWWAN:// 使用3G网络 result=@"3g"; break; case ReachableViaWiFi:// 使用WiFi网络 result=@"wifi"; break; } return result;}
例子:
//// ViewController.h// NetworkStatusDemo//// Created by Fox on 12-3-15.// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import #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// NetworkStatusDemo//// Created by Fox on 12-3-15.// 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
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- iPhone开发 判断当前的网络是3g还是wifi
- 判断当前的网络是3g还是wifi
- iphone 判断当前为3g还是wifi网络
- Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类
- Android:判断当前网络是WIFI还是手机网络
- IOS开发之判断IPHONE连接的网络(WIFI,2G,3G,4G)是IPV4还是IPV6网络
- 判断是否连接到网络,如果有网,是3G还是WIFI
- iOS开发之判断具体当前网络类型(2G网络还是3G网络)
- iPhone开发 判断当前的网络
- Android 判断网络是GPRS还是WIFI
- Android中判断有无可用网络的代码(是否是3G或者WIFI网络)
- 判断是2G网络还是3G网络
- list的sort问题
- 关于中国的第四代中型战斗机
- windows7下双击jar文件运行的设置方法
- 关于依赖注入、Aop
- 蝴蝶兰与鳞托菊
- iPhone开发 判断当前的网络是3g还是wifi
- CSND最新公布视频教程
- Unity3d摄像机的平滑过渡
- 关于链式表达式
- ThinkPHP3.1主入口文件及配置
- Cocos2D-XNA入门篇
- 为vsftpd 本地用户指定目录
- 心醉七夕
- Flash 屏幕共享技术