您的位置:首页 > 移动开发 > IOS开发

IOS Reachability判断所请求服务器是否超时?

2013-07-05 14:17 211 查看
开发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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐