检测是否在Wifi环境下,写成单例判断
#import <Foundation/Foundation.h>
#import "ASIFormDataRequest.h"
#import "Reachability.h"
@interface HttpHelper : NSObject
{
Reachability* internetReach;
NetworkStatus netStatus;
}
+ (id) sharedHttpHelper;
- (BOOL)checkIsWifi;
@end
///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// /////////
#import "HttpHelper.h"
@implementation HttpHelper
static HttpHelper *_sharedInst = nil;
//
+ (id) sharedHttpHelper
{
@synchronized(self){
if(_sharedInst == nil)
{
_sharedInst = [[self alloc] init];
}
}
return_sharedInst;
}
-(id) init
{
if (self = [super init]) {
//通知 (网络状态变化)
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
internetReach = [[ReachabilityreachabilityForInternetConnection]retain];
[internetReachstartNotifier];
netStatus = [internetReachcurrentReachabilityStatus];
}
returnself;
}
//检测网络是否在WiFi条件下
- (BOOL)checkIsWifi//检测是否在wifi下
{
if (netStatus == ReachableViaWiFi) {//网络状态是否是wifi下,如果是返回YES
return YES;
}
returnNO;
}
- (void) reachabilityChanged: (NSNotification* )note
{
netStatus = [internetReachcurrentReachabilityStatus];
}
@end