Ios 难以使用AFNetworking(AFClient)检查网络是否可用
我正在我的应用程序中使用AfNetworking 1.0。 我已经创建了一个AFHTTPClient的单例类。 以下是我的代码Ios 难以使用AFNetworking(AFClient)检查网络是否可用,ios,objective-c,cocoa,afnetworking,ipad-2,Ios,Objective C,Cocoa,Afnetworking,Ipad 2,我正在我的应用程序中使用AfNetworking 1.0。 我已经创建了一个AFHTTPClient的单例类。 以下是我的代码 + (id)sharedInstance { static WebServices *__sharedInstance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSString *WebServiceBaseURLString = [[NSUserDefaults
+ (id)sharedInstance {
static WebServices *__sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *WebServiceBaseURLString = [[NSUserDefaults standardUserDefaults]stringForKey:@"URL"];
__sharedInstance = [[WebServices alloc] initWithBaseURL:[NSURL URLWithString:WebServiceBaseURLString]];
});
return __sharedInstance;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
NSString *accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"UserToken"];
NSLog(@"accessToken=%@",accessToken);
[self setAuthorizationHeaderWithToken:accessToken];
NSLog(@"self=%@",self);
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
self.parameterEncoding = AFJSONParameterEncoding;
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"application/json"]];
}
return self;
}
它的工作文件
但现在我想使用networkreachabilitystatus块
检查网络
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
NSLog(@"changed %d", status);
}];
有谁能告诉我如何使用NetworkReachabilityStatus块吗?- (void)startMonitoringWithStatusChangeBlock:(void(^)(网络可达性状态))块
{
[自我监测]
if(!self.networkReachability){
返回;
}
__弱(自我)弱自我=自我;
AFNetworkReachibilityStatus回调=^(AFNetworkReachibilityStatus状态){
__strong _类型(弱自我)strong自我=弱自我
}) SCNetworkReachabilityContext上下文={0,(u桥空*)回调,AFNetworkReachabilityRetainCallback,AFNetworkReachabilityReleaseCallback,NULL} scnetworkreachabilitycallback(self.networkReachability、AFNetworkReachabilityCallback和context) SCNetworkReachabilityFlags标志 SCNetworkReachabilityGetFlags(self.networkReachability和flags) dispatch\u async(dispatch\u get\u main\u queue()^{ AFNetworkReachibilityStatus状态=AFNetworkReachibilityStatusForFlags(标志); 回调(状态); })) SCNetworkReachibilityScheduleWithRunLoop(self.networkReachibility,CFRunLoopGetMain(),kCFRunLoopCommonModes)strongSelf.networkReachabilityStatus = status; if (block) { block(status); }
}我正在使用AFNetworking 2.0。在viewDidLoad中的类A中尝试编写以下内容:[[AFNetworkReachabilityManager sharedManager]startMonitoring];然后在调用类之前,请执行以下操作:[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus状态){NSLog(@“Reachability:%@”,AFStringFromNetworkReachabilityStatus(状态));};它的状态是枚举的,但我使用的是AFNetworking版本1.0