• iPhone开发技巧之网络篇(4) 确认网络环境 3G/WIFI(转)


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

    Reachability

    Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

     

     

     

     

     

    <IGNORE_JS_OP>Reachability1.png

     

    下载附件 (12.79 KB)

     

     

     

     

     

     




    然后将 SystemConfiguration.framework 添加进工程:

     

     

     

     

     

    <IGNORE_JS_OP>Reachability2.png

     

    下载附件 (14.25 KB)

     

     

     

     

     

     




    Reachability 中定义了3种网络状态。

     

    1. // the network state of the device for Reachability 1.5.
    2. typedef enum {
    3.     NotReachable = 0,
    4.     ReachableViaCarrierDataNetwork,
    5.     ReachableViaWiFiNetwork
    6. } NetworkStatus;
    7. // the network state of the device for Reachability 2.0.
    8. typedef enum {
    9.     NotReachable = 0,
    10.     ReachableViaWiFi,
    11.     ReachableViaWWAN
    12. } NetworkStatus;
    复制代码

     

    NotReachable

    无连接

    ReachableViaCarrierDataNetwork (ReachableViaWWAN)

    使用3G/GPRS网络

    ReachableViaWiFiNetwork (ReachableViaWiFi)

    使用WiFi网络




    比如检测某一特定站点的接续状况,可以使用下面的代码:

    1. Reachability *r = [Reachability reachabilityWithHostName:@“[url]www.apple.com[/url]”];
    2. switch ([r currentReachabilityStatus]) {
    3.     case NotReachable:
    4.         // 没有网络连接
    5.         break;
    6.     case ReachableViaWWAN:
    7.         // 使用3G网络
    8.         break;
    9.     case ReachableViaWiFi:
    10.         // 使用WiFi网络
    11.         break;
    12. }
    复制代码


    检测当前网络环境

    程序启动时,如果想检测可用的网络环境,可以像这样。

     

    1. // 是否wifi
    2. + (BOOL) IsEnableWIFI {
    3.     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
    4. }
    5. // 是否3G
    6. + (BOOL) IsEnable3G {
    7.     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
    8. }
    复制代码

    连接状态实时通知

    网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户。由于Reachability1.5版与2.0版有一些变化,这里分开来说明使用方法。

    Reachability 1.5

    1. // My.AppDelegate.h
    2. #import "Reachability.h"
    3. @interface MyAppDelegate : NSObject  {
    4.     NetworkStatus remoteHostStatus;
    5. }
    6. @property NetworkStatus remoteHostStatus;
    7. @end
    8. // My.AppDelegate.m
    9. #import "MyAppDelegate.h"
    10. @implementation MyAppDelegate
    11. @synthesize remoteHostStatus;
    12. // 更新网络状态
    13. - (void)updateStatus {
    14.     self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
    15. }
    16. // 通知网络状态
    17. - (void)reachabilityChanged:(NSNotification *)note {
    18.     [self updateStatus];
    19.     if (self.remoteHostStatus == NotReachable) {
    20.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)
    21.         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    22.         [alert show];
    23.         [alert release];
    24.     }
    25. }
    26. // 程序启动器,启动网络监视
    27. - (void)applicationDidFinishLaunching:(UIApplication *)application {
    28.     // 设置网络检测的站点
    29.     [[Reachability sharedReachability] setHostName:@"www.apple.com"];
    30.     [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
    31.     // 设置网络状态变化时的通知函数
    32.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
    33.                                                  name:@"kNetworkReachabilityChangedNotification" object:nil];
    34.     [self updateStatus];
    35. }
    36. - (void)dealloc {
    37.     // 删除通知对象
    38.     [[NSNotificationCenter defaultCenter] removeObserver:self];
    39.     [window release];
    40.     [super dealloc];
    41. }
    复制代码


    Reachability 2.0

     

    1. // MyAppDelegate.h
    2. @class Reachability;
    3. @interface MyAppDelegate : NSObject  {
    4.     Reachability  *hostReach;
    5. }
    6. @end
    7. // MyAppDelegate.m
    8. - (void)reachabilityChanged:(NSNotification *)note {
    9.     Reachability* curReach = [note object];
    10.     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    11.     NetworkStatus status = [curReach currentReachabilityStatus];
    12.     if (status == NotReachable) {
    13.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
    14.                                                         message:@"NotReachable"
    15.                                                        delegate:nil
    16.                                               cancelButtonTitle:@"YES" otherButtonTitles:nil];
    17.         [alert show];
    18.         [alert release];
    19.     }
    20. }
    21. - (void)applicationDidFinishLaunching:(UIApplication *)application {
    22.     // ...
    23.     // 监测网络情况
    24.     [[NSNotificationCenter defaultCenter] addObserver:self
    25.                                           selector:@selector(reachabilityChanged:)
    26.                                           name: kReachabilityChangedNotification
    27.                                           object: nil];
    28.     hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
    29.     [hostReach startNotifer];
    30.     // ...
    31. }
    复制代码








  • 相关阅读:
    浏览器是怎样工作的二:渲染引擎 HTML解析(1)(转)
    凯文.都迪的超级记忆力训练教程
    程序员的修炼之道
    我编程我快乐——程序员的职业规划
    SQL Server 数据库备份和还原认识和总结(一)
    SQL Server 管理数据收集
    汇总SQL Server里的相关运算符、子句、谓词等
    SQL Server 数据库备份和还原认识和总结(二)
    解决报表控件报CS0433错误
    通过笔记本配件,理解抽象类接口和委托事件
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2955669.html
Copyright © 2020-2023  润新知