iOS中使用 Reachability 检测网络

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"开启 www.apple.com 的网络检测");  
  5.      Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];  
  6.      DLog(@"-- current status: %@", reach.currentReachabilityString);  
  7.       
  8.      // start the notifier which will cause the reachability object to retain itself!  
  9.       
  10.      [[NSNotificationCenter defaultCenter] addObserver:self  
  11.                                                         selector:@selector(reachabilityChanged:)  
  12.                                                              name:kReachabilityChangedNotification  
  13.                                                           object:nil];  
  14.       
  15.      reach.reachableBlock = ^(Reachability * reachability)  
  16.     {  
  17.         dispatch_async(dispatch_get_main_queue(), ^{  
  18.             self.blockLabel.text = @"网络可用";  
  19.                self.blockLabel.backgroundColor = [UIColor greenColor];  
  20.         });  
  21.     };  
  22.      
  23.     reach.unreachableBlock = ^(Reachability * reachability)  
  24.     {  
  25.         dispatch_async(dispatch_get_main_queue(), ^{  
  26.             self.blockLabel.text = @"网络不可用";  
  27.                self.blockLabel.backgroundColor = [UIColor redColor];  
  28.         });  
  29.     };  
  30.       
  31.      [reach startNotifier];  
  32. }  
  33.    

 

 

使用notification的方式

 

C代码   收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      DLog(@"开启 www.apple.com 的网络检测");  
  5.      Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];  
  6.      DLog(@"-- current status: %@", reach.currentReachabilityString);  
  7.       
  8.      // start the notifier which will cause the reachability object to retain itself!  
  9.       
  10.      [[NSNotificationCenter defaultCenter] addObserver:self  
  11.                                                         selector:@selector(reachabilityChanged:)  
  12.                                                              name:kReachabilityChangedNotification  
  13.                                                           object:nil];  
  14.      [reach startNotifier];  
  15. }  
  16.   
  17. - (void) reachabilityChanged: (NSNotification*)note {  
  18.      Reachability * reach = [note object];  
  19.      
  20.     if(![reach isReachable])  
  21.     {  
  22.         self.notificationLabel.text = @"网络不可用";  
  23.           self.notificationLabel.backgroundColor = [UIColor redColor];  
  24.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];  
  25.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];  
  26.           return;  
  27.     }  
  28.          
  29.      self.notificationLabel.text = @"网络可用";  
  30.      self.notificationLabel.backgroundColor = [UIColor greenColor];  
  31.       
  32.      if (reach.isReachableViaWiFi) {  
  33.           self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];  
  34.           self.wifiOnlyLabel.text = @"当前通过wifi连接";  
  35.      } else {  
  36.           self.wifiOnlyLabel.backgroundColor = [UIColor redColor];  
  37.           self.wifiOnlyLabel.text = @"wifi未开启,不能用";  
  38.      }  
  39.       
  40.      if (reach.isReachableViaWWAN) {  
  41.           self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];  
  42.           self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";  
  43.      } else {  
  44.           self.wwanOnlyLabel.backgroundColor = [UIColor redColor];  
  45.           self.wwanOnlyLabel.text = @"2g or 3g网络未使用";  
  46.      }  
  47. }  

 

 

附件demo说明

开启wifi状态


 关闭wifi的状态


 

 

 

遗留问题

  1. 如何在多个controller之前共用一个Reachability(附件demo中是一个controller一个Reachability实例)
  2. 应该在什么使用停止Reachability的检测.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,您的问题不太清晰,请问您是想实现iOS应用程序首次打开时弹出网络连接提示框吗?如果是的话,可以参考以下步骤: 1. 在您的应用程序添加一个网络检测功能,判断当前设备是否联网。可以使用Reachability库实现。 2. 在应用程序的AppDelegate文件添加以下代码,在应用程序启动时检测网络状态,并根据需要显示提示框: ```swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 检测网络状态 let reachability = Reachability.forInternetConnection() let status = reachability?.currentReachabilityStatus() // 如果网络不可用,弹出提示框 if status == NotReachable { let alertController = UIAlertController(title: "网络连接错误", message: "请检查您的网络连接", preferredStyle: .alert) let okAction = UIAlertAction(title: "确定", style: .default, handler: nil) alertController.addAction(okAction) self.window?.rootViewController?.present(alertController, animated: true, completion: nil) } return true } ``` 3. 为了避免每次启动应用程序都弹出提示框,您可以使用UserDefaults来保存用户是否已经看过提示框的状态。只有在用户第一次启动应用程序时才弹出提示框。 ```swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 检测网络状态 let reachability = Reachability.forInternetConnection() let status = reachability?.currentReachabilityStatus() // 如果网络不可用,弹出提示框 if status == NotReachable { let userDefaults = UserDefaults.standard let hasShownAlert = userDefaults.bool(forKey: "HasShownNetworkAlert") if !hasShownAlert { let alertController = UIAlertController(title: "网络连接错误", message: "请检查您的网络连接", preferredStyle: .alert) let okAction = UIAlertAction(title: "确定", style: .default, handler: nil) alertController.addAction(okAction) self.window?.rootViewController?.present(alertController, animated: true, completion: nil) userDefaults.set(true, forKey: "HasShownNetworkAlert") userDefaults.synchronize() } } return true } ``` 希望这些步骤能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值