您的位置:首页 > 理论基础 > 计算机网络

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

2014-07-02 17:41 204 查看
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.检测手机是否能上网络(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: