开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。

Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。

1.在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。

2.然后将 SystemConfiguration.framework 添加进工程。

我使用的版本为 : Version: 2.2

我为Apple的例程增加了一个全局 -- ReachabilityAutoChecker

.h

@interface ReachabilityAutoChecker : NSObject

@property (nonatomic, retain) Reachability  *reachability;
@property (nonatomic, assign) NetworkStatus networkStatus;
@property (nonatomic, assign) BOOL connectionRequired; @end

.m文件

@implementation ReachabilityAutoChecker

@synthesize reachability;

+ (id)sharedChecker
{
static ReachabilityAutoChecker *staticChecker = nil;
if (!staticChecker) {
staticChecker = [[ReachabilityAutoChecker alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:staticChecker selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
staticChecker.networkStatus = NotReachable;
staticChecker.connectionRequired = NO;
}
return staticChecker;
} - (void)reachabilityChanged:(NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
self.networkStatus = [curReach currentReachabilityStatus];
self.connectionRequired = [curReach connectionRequired];
} @end

我为Apple的例程增加了一个Category -- Reachability (AutoChecker)

.h文件:

@interface Reachability (AutoChecker)

+ (void)startCheckWithReachability:(Reachability *)reachability;
+ (BOOL)isReachable; @end

.m文件:

@implementation Reachability (AutoChecker)

+ (void)startCheckWithReachability:(Reachability *)reachability
{
ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker]; if (checker.reachability) {
[checker.reachability stopNotifier];
checker.reachability = nil;
} checker.reachability = reachability;
[checker.reachability startNotifier];
} + (BOOL)isReachable
{
ReachabilityAutoChecker *checker = [ReachabilityAutoChecker sharedChecker]; if (!checker.reachability) {
NSLog(@"Check Reachability With No Reachability has been Set!");
return NO;
} NetworkStatus networkStatus = [checker networkStatus]; if(networkStatus == ReachableViaWiFi)
{
NSLog(@"WIFI");
}
if(networkStatus == ReachableViaWWAN)
{
NSLog(@"3G");
} BOOL connectionRequired = NO;
connectionRequired = [checker connectionRequired]; #if kShouldPrintReachabilityFlags
NSLog(@"NetworkStatus %d connectionRequired %d", networkStatus, connectionRequired);
#endif if (networkStatus)
return YES;
else
return NO;
} @end

调用方式:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//检测某一特定站点的接续状况,可以使用下面的代码
Reachability *pReachability = [Reachability reachabilityWithHostName:@"appservices.comcsoft.com"];
//开始监控网络状态
[Reachability startCheckWithReachability:pReachability]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

ViewController.m

- (IBAction)upInside_checkNetStatus:(id)sender
{
if(![Reachability isReachable])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"对不起,网络异常,请检查您的网络设置。"
delegate:self
cancelButtonTitle:@"好的"
otherButtonTitles:nil];
[alert show];
return;
}
}

运行时提醒下 :  #error "此类需要在非arc环境下编译,请添加-fno-objc-arc标记"

网络正常 NSLog如下:

2013-07-05 14:15:53.084 PRJ_reachability[8153:11303] Reachability Flag Status: -R ------- networkStatusForFlags

2013-07-05 14:15:54.265 PRJ_reachability[8153:11303] WIFI

2013-07-05 14:15:54.266 PRJ_reachability[8153:11303] NetworkStatus 1 connectionRequired 0

PRJ_reachability.zip 例子下载地址:http://ishare.iask.sina.com.cn/f/37441462.html

百度云盘分享链接:http://pan.baidu.com/s/1o6qhQCa

IOS Reachability判断所请求服务器是否超时?的更多相关文章

  1. iOS中4种判断网络请求的方式(系统状态栏、AFNetworking、Reachability、自定义)

    iOS 实时判断网络状态 方法一:利用系统状态栏判断网络状态 // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication shared ...

  2. IOS Post请求(请求服务器)

    @interface HMViewController () @property (weak, nonatomic) IBOutlet UITextField *usernameField; @pro ...

  3. IOS 请求服务器的方式

    IOS 中请求服务器的方式主要有Get 和Post . Get :[1]向服务器发索取数据的一种请求; [2]获取信息,而不是修改信息,类似数据库查询功能一样,数据不会被修改;请求的参数会跟在url后 ...

  4. Android使用HttpUrlConnection请求服务器发送数据详解

    HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...

  5. Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)

    讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...

  6. PHP异步非阻塞fsockopen(本地可以非阻塞请求,服务器就一直执行异步的不成功) (未解决)

    index.php /** * php异步请求 * * @param $host string 主机地址 * @param $path string 路径 * @param $param array ...

  7. Java通过Http请求服务器

    Java通过Http请求服务器图片输出.下载.转换 Java开发过程中总会遇到从服务器中请求文件(图片.text文档等).此处详情记录从服务器下载图片的方法,以及以多种方式输出. 1.整体流程: 建立 ...

  8. HttpClient请求服务器代码优化版

    HttpClient请求服务器代码优化版 首先,我在前面的两篇博文中介绍了在 Android中,除了使用java.net包下HttpUrlConnection的API访问HTTP服务之外,我们还可以换 ...

  9. PHP判断ajax请求:HTTP_X_REQUESTED_WITH

    PHP判断ajax请求的原理: 在发送ajax请求的时候,我们可以通过XMLHttpRequest这个对象,创建自定义的 header头信息, 在jquery框架中,对于通过它的$.ajax, $.g ...

随机推荐

  1. 自动安装jar包到本地仓库

    参考博客:http://blog.csdn.net/m0_37797991/article/details/73394873

  2. python--tesseract

    tesseract的介绍 我们爬虫会受到阻碍,其中一个便是我们在模拟登陆或者请求一些数据的时候,出现的图形验证码,因此我们需要一种能叫图形验证码识别成文本的技术.将图片翻译成文字一般称为光学文字识别( ...

  3. python 作业

    Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...

  4. centos 时间日期设置

    date  时间窗口 date -s '2015-02-02 10:10:00'  更改年月日小时分秒 date -s 10:00:02  只更改时间 不更改年月 clock -w 写入系统时间 hw ...

  5. CentOS 6.7下配置 yum 安装 Nginx

    CentOS 6.7下配置 yum 安装 Nginx. 转载:http://www.linuxidc.com/Linux/2016-07/133283.htm 第一步,在/etc/yum.repos. ...

  6. js禁止页面所有a链接访问

    <script type="text/javascript"> var arr=document.getElementsByTagName("a") ...

  7. 如何严格设置php中session过期时间 (转)

    如何严格限制session在30分钟后过期!1.设置客户端cookie的lifetime为30分钟:2.设置session的最大存活周期也为30分钟:3.为每个session值加入时间戳,然后在程序调 ...

  8. Ghostscript 中 ps2pdf 命令在 windows msys 下的运行错误问题。

    前两天看到了 miloyip/game-programmer 这个项目觉得特别有用,真是好东西,明确了指出了学习路线,尤其是新手.不过打开看,有些书对应的亚马逊链接是无效的,比如<Tricks ...

  9. Linux命令之ping

    ping [选项] destination ping命令向网络主机发送ICMP回传请求 详细描述:ping使用ICMP协议强制ECHO_REQUEST(回传请求)数据报从主机或网关获取ICMP协议的E ...

  10. hdu 5961 传递 (2016ccpc 合肥站 A题)

    传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...