• ios网络开发 网络状态检查


    http://www.cnblogs.com/hanjun/archive/2012/12/01/2797622.html

    网络连接中用到的类:

    一.Reachability 

        1.添加 Reachability 的.h和.m文件,再添加SystemConfiguration.framework。

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

      typedef Num{

    NotReachable = 0,  //无连接

    ReachableViaWiFi,  //使用3G/GPRS网络

    ReachableViaWWAN   //使用WiFi网络

           }NetworkStatus;

         3.示例:

      Reachability *reachability = [Reachablity  reachabilityWithHostName:@"www.baidu.com"];

      switch([reachabilityStatus]){

    case  NotReachable:

    //TODO 

    break; 

    case  ReachableViaWiFi:

    //TODO  

    break; 

    case  ReachableViaWWAN:

    //TODO  

    break;  

     } 

          4.检查当前网络环境

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

      //是否wifi

    + (BOOL)isEnableWIFI 

    {

    return ([[Reachability reachabiliyForLocalWIFI] currentReachabilityStatus] != NotReachable); 

       }

       //是否3G

    + (BOOL)isEnable3G

    {

    return ([[Reachability reachabiliyForInternetConnetion] currentReachabilityStatus] != NotReachable); 

       }

       示例:

    - (void)viewWillAppear:(BOOL)animated


      if (([Reachability reachabiliyForInternetConnetion].currentReachabilityStatus == NotReachable) && [Reachability                         reachabiliyForLocalWIFI].currentReachabilityStatus == NotReachable))

    {

    self.navigationItem.hidesBackButton = YES;

    [self.navigationItem setLeftBarButtonItem:nil animated:NO]; 


     } 

           5.链接状态的实时通知

    实时检查,持续状态发生变化时,需要及时地通知用户:

    复制代码
    Reachability 1.5版本
    //MyAppDelegate.h

    #import "Reachability"

    @interface MyAppDelegate:NSObject<UIApplicationDelegate>
    {
        
    }

    @property NetworkStatus remoteHostStatus;

    @end 
    复制代码

     //MyAppDelegate.m

    复制代码

    #import "MyAppDelegate.h"

    @implementation MyAppDelegate
    @synthesize remoteHostStatus;

    //更新网络状态
    - (void)updateStatus
    {
        self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
    }

    //通知网络状态
    - (void)reachabilityChanged:(NSNotification *)note
    {
        [self updateStatus];
        if (self.remoteHostStatus == NotReachable)
       {
           UIAlert *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName",nil)
      message: NSLocalizedString (@"NotReachable",nil);
      delegate:nil cancelButtonTitle:@"OK" 
      otherButtonTitles:nil];

       [alert show];
       [alert release];
        }
    }


    //程序启动器,启动网络监视
    - (void)applicationDidFinishLaunching:(UIApplication *)application
    {
       //设置网络监测的站点
       [[Reachability sharedReachability] setHostName:@"www.baidu.com"];
       [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];

       //设置网络状态变化时的通知函数
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) 
    name:@"kNetworkReachabilityChangedNotification" object:nil];
       [self updateStatus];


    }

    - (void)dealloc
    {
        //删除通知对象
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [window release];
        [super dealloc];
    }
    复制代码
    复制代码
     Reachability 2.0版本
    //MyAppDelegate.h

    #import "Reachability"
    @class Reachability;
    @interface MyAppDelegate:NSObject<UIApplicationDelegate>
    {
         Reachability *hostReach;
    }


    @end 
     
     //MyAppDelegate.m

    #import "MyAppDelegate.h"

    @implementation MyAppDelegate

    //通知网络状态
    - (void)reachabilityChanged:(NSNotification *)note
    {
        Reachability *currentReach = [note object];
        NSParameterAssert([currentReach isKindOfClass:[Reachability class]]);
        NetworkStatus status = [currentReach currentReachabilityStatus]; 

        if (status == NotReachable)
       {
           UIAlert *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName",nil)
      message: NSLocalizedString (@"NotReachable",nil);
      delegate:nil cancelButtonTitle:@"YES" 
      otherButtonTitles:nil];

       [alert show];
       [alert release];
        }
    }


    //程序启动器,启动网络监视
    - (void)applicationDidFinishLaunching:(UIApplication *)application
    {
       //....

       //监测网络情况
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) 
    name:@"kNetworkReachabilityChangedNotification" object:nil];
      hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
     // hostReach startNotifer]; 
       //...


    }
    复制代码

    二、其他常用的类。

     1.NSURL

     2.NSURLRequest

     3.NSMutableURLRequest 是NSURLRequest的子类,可以设置一些请求参数

     4.NSURLResponse 

     5.NSError 

  • 相关阅读:
    启动Tomcat报错:严重: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
    数学 交换律
    android中LayoutInflater的使用
    数学 三角函数
    数学 矩阵
    数学 弧度
    数学 分配律
    Android开发必备的免费资源和工具
    数学 结合律
    View.setTag()的作用
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4469624.html
Copyright © 2020-2023  润新知