[英]Getting difficulty in using the AFNetworking(AFClient) for checking the network is available or not
我在我的應用程序中使用了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塊?
(void)startMonitoringWithStatusChangeBlock:(void(^)(AFNetworkReachabilityStatus status))block {
[自我停止監視];
如果(!self.networkReachability){返回; }
__weak __typeof(self)weakSelf =自我; AFNetworkReachabilityStatusBlock回調= ^(AFNetworkReachabilityStatus狀態){__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.networkReachabilityStatus = status; if (block) { block(status); }
};
SCNetworkReachabilityContextContext 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); callback(status);});
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability,CFRunLoopGetMain(),kCFRunLoopCommonModes);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.