Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ios网络开发 网络状态检查

Posted on   gavin.han  阅读(3933)  评论(0编辑  收藏  举报

网络连接中用到的类:

一.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 

 

 

 

 

 

 

 

 

 

 

 

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示