iPhone开发 判断当前的网络是3g还是wifi

1.添加framework:

将SystemConfiguration.framework 添加进工程。

2.下载Reachability

https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 

复制里面的Reachability.h和Reachability.m到项目中。

3.调用代码:

[csharp]  view plain copy
  1. //判断当前的网络是3g还是wifi  
  2.   
  3. -(NSString*)GetCurrntNet  
  4.   
  5. {  
  6.   
  7.     NSString* result;  
  8.   
  9.     Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];  
  10.   
  11.     switch ([r currentReachabilityStatus]) {  
  12.   
  13.         case NotReachable:// 没有网络连接  
  14.   
  15.             result=nil;  
  16.   
  17.             break;  
  18.   
  19.         case ReachableViaWWAN:// 使用3G网络  
  20.   
  21.             result=@"3g";  
  22.   
  23.             break;  
  24.   
  25.         case ReachableViaWiFi:// 使用WiFi网络  
  26.   
  27.             result=@"wifi";  
  28.   
  29.             break;  
  30.   
  31.     }  
  32.   
  33.     return result;  
  34.   
  35. }  

例子:

[csharp]  view plain copy
  1. //  
  2. //  ViewController.h  
  3. //  NetworkStatusDemo  
  4. //  
  5. //  Created by Fox on 12-3-15.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.  
  9. #import   
  10. #import "Reachability.h"  
  11.   
  12. @interface ViewController : UIViewController{  
  13.     IBOutlet UILabel *netstatus;  
  14.       
  15.     Reachability* status; //网络状态  
  16.   
  17. }  
  18. @property (retain, nonatomic) IBOutlet UILabel *netstatus;  
  19. @property (retain, nonatomic) Reachability* status;  
  20.   
  21. + (BOOL) IsEnableWIFI;  
  22. + (BOOL) IsEnable3G;  
  23.   
  24. @end  

[csharp]  view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  NetworkStatusDemo  
  4. //  
  5. //  Created by Fox on 12-3-15.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.  
  9. #import "ViewController.h"  
  10. #import "Reachability.h"  
  11.   
  12. @implementation ViewController  
  13. @synthesize netstatus;  
  14. @synthesize status;  
  15.   
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.       
  21.     self.status = [[Reachability alloc] init];  
  22.     status = [Reachability reachabilityWithHostName:@"www.cnblogs.com/foxmin"];  
  23.     switch ([status currentReachabilityStatus]) {  
  24.         case NotReachable:  
  25.             // 没有网络连接  
  26.             self.netstatus.text = @"没有网络连接";  
  27.             break;  
  28.         case ReachableViaWWAN:  
  29.             // 使用3G网络  
  30.             self.netstatus.text = @"使用3G网络";  
  31.             break;  
  32.         case ReachableViaWiFi:  
  33.             // 使用WiFi网络  
  34.             self.netstatus.text = @"使用WiFi网络";  
  35.             break;  
  36.     }  
  37.       
  38.     //程序启动时,检查程序的网络环境  
  39.     if ([ViewController IsEnableWIFI] && ![ViewController IsEnable3G]) {  
  40.         self.netstatus.text = @"使用WiFi网络";  
  41.     }else if(![ViewController IsEnableWIFI] && [ViewController IsEnable3G]){  
  42.         self.netstatus.text = @"使用3G网络";  
  43.     }else  self.netstatus.text = @"没有网络连接";  
  44.       
  45. }  
  46.   
  47. - (void)viewDidUnload  
  48. {  
  49.     [self setNetstatus:nil];  
  50.     [super viewDidUnload];  
  51.     // Release any retained subviews of the main view.  
  52.     // e.g. self.myOutlet = nil;  
  53. }  
  54.   
  55. - (void)dealloc {  
  56.     [netstatus release];  
  57.     [super dealloc];  
  58. }  
  59.   
  60. /* 
  61.  *判断是否通过wifi 
  62.  */  
  63. + (BOOL) IsEnableWIFI {  
  64.     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);  
  65. }  
  66.   
  67. /* 
  68.  *判断是否通过3G 
  69.  */  
  70. + (BOOL) IsEnable3G {  
  71.     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);  
  72. }  
  73.   
  74. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值