开发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. python爬虫模块之URL管理器模块

    URL管理器模块 一般是用来维护爬取的url和未爬取的url已经新添加的url的,如果队列中已经存在了当前爬取的url了就不需要再重复爬取了,另外防止造成一个死循环.举个例子 我爬www.baidu. ...

  2. 【bzoj1026】windy数

    江泽OJ好,远离bzoj保平安. 仍然沿用之前的记忆化搜索的办法即可. #include<bits/stdc++.h> #define N 10010 using namespace st ...

  3. C++ 模板的用法

    C++中的高阶手法就会用到泛型编程,主要有函数模板, 在程序中使用模板的好处就是在定义时不需要指定具体的参数类型,而在使用时确可以匹配其它任意类型, 定义格式如下 template <class ...

  4. 给mongodb设置密码权限

    昨天装了个mongodb数据库用于测试用,装好后没有密码,现在就讲讲怎么设置密码 1.首先进入C:\mongodb\bin下面运行mongod.exe启动数据库. 2.在相同目录下启动mongo.ex ...

  5. C# Merge into的使用详解

    Merge是一个非常有用的功能,类似于Mysql里的insert into on duplicate key. Oracle在9i引入了merge命令, 通过这个merge你能够在一个SQL语句中对一 ...

  6. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  7. CentOS7配置sentinel高可用redis

    redis哨兵:用于管理和实现多个redis组实现高可用,sentinel哨兵只监控主节点,因为主节点上有所有的从节点信息,当master节点发生故障,sentinel之间会进行投票选举一个slave ...

  8. 编写简单的spring mvc程序,在tomcat上部署

    编写简单的spring mvc程序,在tomcat上部署 1 用java 配置spring mvc ,可以省去web.xmlpackage hello;import org.springframewo ...

  9. centos系统服务管理

    系统服务管理工具: chkconfig(所有linux发行版都有),用法很简单,如下: usage:   chkconfig --list [name]          chkconfig --ad ...

  10. Python 2.7.13安装

    参考文章:安装Python 进入至Python官方网站,点击下载 下载完成后直接进行安装 选择安装的路径 选择安装的组件,请注意选择安装pip和Add python.exe to Path这两个选项 ...