zoukankan      html  css  js  c++  java
  • 网络编程练习 -- 检测网络状态

    LWTViewController.m

    //
    //  LWTViewController.m
    //  网络编程练习 -- 检测网络状态
    //
    //  Created by apple on 14-7-2.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTViewController.h"
    #import "Reachability.h"
    
    @interface LWTViewController ()
    @property (nonatomic, strong) Reachability *wann;
    @end
    
    @implementation LWTViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
        
        self.wann = [Reachability reachabilityForInternetConnection];
        
        [self.wann startNotifier];
        
    }
    
    - (void)dealloc
    {
        [self.wann stopNotifier];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)networkStateChange
    {
        // 1.检测wifi状态
        Reachability *wifi = [Reachability reachabilityForLocalWiFi];
        
        // 2.检测手机是否能上网络(WIFI3G2.5G)
        Reachability *wann = [Reachability reachabilityForInternetConnection];
        
        // 3.判断网络状态
        if (wifi.currentReachabilityStatus != NotReachable) {
            NSLog(@"有wifi");
        }else if([wann currentReachabilityStatus] != NotReachable)
        {
            NSLog(@"使用手机自带网络进行上网");
        }else
        {
            NSLog(@"没有网络");
        }
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // 1.检测wifi状态
        Reachability *wifi = [Reachability reachabilityForLocalWiFi];
        
        // 2.检测手机是否能上网络(WIFI3G2.5G)
        Reachability *wann = [Reachability reachabilityForInternetConnection];
        
        // 3.判断网络状态
        if (wifi.currentReachabilityStatus != NotReachable) {
            NSLog(@"有wifi");
        }else if([wann currentReachabilityStatus] != NotReachable)
        {
            NSLog(@"使用手机自带网络进行上网");
        }else
        {
            NSLog(@"没有网络");
        }
    
    }
    
    
    
    @end
    View Code
  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3820780.html
Copyright © 2011-2022 走看看