如何在ios中检测网络连接

本文介绍如何使用Apple提供的Reachability类来监测iOS设备上的网络连接状态。通过简单的步骤,开发者可以轻松地检查设备是否连接到互联网,并判断是通过WiFi还是蜂窝数据连接。此外,还介绍了如何监听特定主机的可达性。
摘要由CSDN通过智能技术生成

注:Reachability.h and Reachability.m是apple提供的示例代码,在xcode的developer document中能找到。

 

 

1) Add SystemConfiguration framework to the project but don't worry about including it anywhere

2) Add Reachability.h and Reachability.m to the project

3) Add @class Reachability; to the .h file of where you are implementing the code

4) Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable;
   
Reachability* hostReachable;

5) Add a method in the .h for when the network status updates:

    - (void) checkNetworkStatus:(NSNotification *)notice;

6) Add #import "Reachability.h" to the .m file where you are implementing the check

7) In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated
   
{
       
// check for internet connection
       
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

        internetReachable
= [[Reachability reachabilityForInternetConnection] retain];
       
[internetReachable startNotifier];

       
// check if a pathway to a random host exists
        hostReachable
= [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
       
[hostReachable startNotifier];

       
// now patiently wait for the notification
   
}

8) Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    - (void) checkNetworkStatus:(NSNotification *)notice
   
{
     
// called after network status changes

     
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
     
switch (internetStatus)
     
{
     
case NotReachable:
     
{
       
NSLog(@"The internet is down.");
        self
.internetActive = NO;
               
break;

         
}
     
case ReachableViaWiFi:
         
{
       
NSLog(@"The internet is working via WIFI.");
        self
.internetActive = YES;
             
break;
         
}
     
case ReachableViaWWAN:
         
{
       
NSLog(@"The internet is working via WWAN.");
        self
.internetActive = YES;
             
break;
           
}
     
}

     
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     
switch (hostStatus)
     
{
     
case NotReachable:
         
{
       
NSLog(@"A gateway to the host server is down.");
        self
.hostActive = NO;
             
break;
         
}
     
case ReachableViaWiFi:
           
{
       
NSLog(@"A gateway to the host server is working via WIFI.");
        self
.hostActive = YES;
               
break;
           
}
     
case ReachableViaWWAN:
           
{
       
NSLog(@"A gateway to the host server is working via WWAN.");
       self
.hostActive = YES;
               
break;
           
}
     
}
   
}

9) In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    - (void) viewWillDisappear:(BOOL)animated
   
{
           
[[NSNotificationCenter defaultCenter] removeObserver:self];
   
}

Note: There might be an instance using viewWillDisappear where you receive a memory warning and the observer never gets unregistered so you should account for that as well.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值