iPhone开发之确认网络环境

来源:互联网 发布:小米云下载软件 编辑:程序博客网 时间:2024/05/16 06:35

开发网络应用程序的时候,需要确认网络环境及连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。


1. 添加源文件及Framework

Apple 的官方例子 Reachability 中介绍了获取、检测设备当前网络状态的方法。在你的程序中,需要把该工程中的Reachability.h 和 Reachability.m 拷贝到你的工程中,同时需要把 SystemConfiguration.framework 添加到工程中,

如下图:



Reachability.h 中定义了三种网络状态:

[cpp] view plaincopyprint?
  1. typedef enum {  
  2.     NotReachable = 0,  
  3.     ReachableViaWiFi,  
  4.     ReachableViaWWAN  
  5. } NetworkStatus;  


1) NotReachable

表示无连接

2) ReachableViaWiFi

使用 WiFi 网络连接

3) ReachableViaWWAN

使用 3G / GPRS网络


2 网络连接状态实时通知

当用户的网络状态发生改变时,需要实时通知用户。代码如下所示:

AppDelegate.h:

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>  
  2. #import "Reachability.h"  
  3.   
  4. @interface AppDelegate : UIResponder <UIApplicationDelegate> {  
  5.     Reachability *_reach;  
  6.     NetworkStatus _status;  
  7. }  
  8.   
  9. @property (strong, nonatomic) UIWindow *window;  
  10.   
  11. @end  

AppDelegate.m:
[cpp] view plaincopyprint?
  1. #import "AppDelegate.h"  
  2.   
  3.   
  4. @implementation AppDelegate  
  5.   
  6. @synthesize window = _window;  
  7.   
  8. - (void)dealloc  
  9. {  
  10.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  11.     [_reach release];  
  12.     [_window release];  
  13.     [super dealloc];  
  14. }  
  15.   
  16. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  17. {  
  18.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  19.     // Override point for customization after application launch.  
  20.     [self startWatchingNetworkStatus];  
  21.     self.window.backgroundColor = [UIColor whiteColor];  
  22.     [self.window makeKeyAndVisible];  
  23.     return YES;  
  24. }  
  25.   
  26. #pragma mark Network Watching  
  27. - (void)startWatchingNetworkStatus {  
  28.     //监测网络状况  
  29.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];  
  30.     _reach = [Reachability reachabilityWithHostName:@"www.google.com"];  
  31.     [_reach startNotifier];  
  32.     _status = ReachableViaWiFi;  
  33. }  
  34.   
  35. - (void)reachabilityChanged:(NSNotification* )note {  
  36.     Reachability *curReach = [note object];  
  37.     NSParameterAssert([curReach isKindOfClass:[Reachability class]]);  
  38.       
  39.     //检测站点的网络连接状态  
  40.     NetworkStatus curStatus = [curReach currentReachabilityStatus];  
  41.     if (curStatus != _status) {  
  42.         NSString *str = nil;  
  43.           
  44.         //根据不同的网络状态,UI或者程序中关于数据的下载需要做出相应的调整,自己实现  
  45.         switch (curStatus) {  
  46.             case NotReachable:  
  47.                 str = @"网络不可用";  
  48.                 break;  
  49.             case ReachableViaWiFi:  
  50.                 str = @"wifi网络可用";  
  51.                 break;  
  52.             case ReachableViaWWAN:  
  53.                 str = @"3G/GPRS网络可用";  
  54.                 break;  
  55.                   
  56.             default:  
  57.                 str = @"未知网络";  
  58.                 break;  
  59.         }  
  60.         NSLog(@"%@", str);  
  61.     }  
  62.       
  63.     _status = curStatus;  
  64. }  
  65. @end  

0 0