IOS Reachability判断所请求服务器是否超时?
开发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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构