码迷,mamicode.com
首页 > 其他好文 > 详细

网络编程练习 -- 检测网络状态

时间:2014-07-03 22:42:58      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   strong   

LWTViewController.m

bubuko.com,布布扣
//
//  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.检测手机是否能上网络(WIFI\3G\2.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.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *wann = [Reachability reachabilityForInternetConnection];
    
    // 3.判断网络状态
    if (wifi.currentReachabilityStatus != NotReachable) {
        NSLog(@"有wifi");
    }else if([wann currentReachabilityStatus] != NotReachable)
    {
        NSLog(@"使用手机自带网络进行上网");
    }else
    {
        NSLog(@"没有网络");
    }

}



@end
View Code

 

网络编程练习 -- 检测网络状态,布布扣,bubuko.com

网络编程练习 -- 检测网络状态

标签:style   blog   http   color   使用   strong   

原文地址:http://www.cnblogs.com/wentianblog/p/3820780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!