我在我的应用中使用AfNetworking 1.0。 我已经创建了一个单独的AFHTTPClient类。 以下是我的代码;
+ (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;
}
其工作档案。
但现在我想使用AFNetworkReachabilityStatus块
检查网络。
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
NSLog(@"changed %d", status);
}];
任何人都可以建议我如何使用AFNetworkReachabilityStatus块吗?
答案 0 :(得分:0)
(void)startMonitoringWithStatusChangeBlock :( void(^)(AFNetworkReachabilityStatus status))block {
[self stopMonitoring];
if(!self.networkReachability){ 返回; }
__弱__typeof(self)weakSelf = self; AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){ __strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.networkReachabilityStatus = status;
if (block) {
block(status);
}
};
SCNetworkReachabilityContext context = {0,(__ bridge void *)callback,AFNetworkReachabilityRetainCallback,AFNetworkReachabilityReleaseCallback,NULL};
SCNetworkReachabilitySetCallback(self.networkReachability,AFNetworkReachabilityCallback,& context);
SCNetworkReachabilityFlags标志;
SCNetworkReachabilityGetFlags(self.networkReachability,& flags);
dispatch_async(dispatch_get_main_queue(),^ { AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); 回调(状态); });
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability,CFRunLoopGetMain(),kCFRunLoopCommonModes);
}