首页 > 代码库 > 网络编程练习 -- 检测网络状态
网络编程练习 -- 检测网络状态
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。