IOS开发之网络编程开源类 Reachability应用
2013-10-24 11:51
309 查看
先看Reachability.h发现
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
所以如果我们在项目中需要用到此类的话,需要引入SystemConfiguration.framework。
此类在ios网络开发中可以确认判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
enum { // DDG NetworkStatus Constant Names.
kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
kReachableViaWiFi
};
定义三种网络类型:
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接
我在项目中用下面的语句判断是否存在网络连接
BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
if (!reachable) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"该功能需要连接网络才能使用,请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]autorelease];
[alertView show];
return;
}
当有网络请求的时候,类中方法可以返回目前的网络连接状态
例如:Reachable *reachable = [Reachable reachabilityWithHostName:@"http://blog.csdn.net"];
之后我们应用
// These are the status tests.
- (NetworkStatus) currentReachabilityStatus; 返回网络连接状态
switch( [reachable currentReachabilityStatus ] ) {
/* [reachable currentReachabilityStatus ]包含三个值
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接 */
}
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
所以如果我们在项目中需要用到此类的话,需要引入SystemConfiguration.framework。
此类在ios网络开发中可以确认判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)
enum { // DDG NetworkStatus Constant Names.
kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
kReachableViaWiFi
};
定义三种网络类型:
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接
我在项目中用下面的语句判断是否存在网络连接
BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
if (!reachable) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"该功能需要连接网络才能使用,请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]autorelease];
[alertView show];
return;
}
当有网络请求的时候,类中方法可以返回目前的网络连接状态
例如:Reachable *reachable = [Reachable reachabilityWithHostName:@"http://blog.csdn.net"];
之后我们应用
// These are the status tests.
- (NetworkStatus) currentReachabilityStatus; 返回网络连接状态
switch( [reachable currentReachabilityStatus ] ) {
/* [reachable currentReachabilityStatus ]包含三个值
一:kNotReachable 无网络连接
二:kReachableViaWWAN 使用GPRS或者3G网络连接
三:kReachableViaWiFi 使用WIFI连接 */
}
相关文章推荐
- IOS开发之网络编程开源类 Reachability应用
- IOS开发之网络编程开源类 Reachability应用
- IOS开发之网络编程开源类 Reachability应用
- IOS开发之网络编程开源类 Reachability应用
- IOS开发——网络监测
- iOS开发:网络参考模型
- iOS开发之网络编程篇二:Get,Post请求差异及用法
- IOS 开发 网络详解(10)--- AFNetWorking概述
- iOS 开发之网络——总结
- iOS多线程与网络开发之NSURLCache
- ios开发进阶之网络05 NSURLConnection AFN
- IOS开发 轻松掌握AFN网络框架
- 学习IOS开发网络多线程篇--NSThread/GCD/
- iOS开发--网络下载
- iOS开发网络—05数据安全
- ios开发之网络访问的数据类型
- ios开发判断网络连接及网络异常(ios自学笔记)
- 【iOS开发】网络请求报错
- iOS开发 - 网络数据安全加密(MD5)