0

I am using AfNetworking 1.0 into my app. I have create a singleton class of AFHTTPClient. following is my code;

 + (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;
 }

its working file.

But now i want to use AFNetworkReachabilityStatus block

to check the network.

 [self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
 NSLog(@"changed %d", status);

 }];

can any one advice me how to use AFNetworkReachabilityStatus block?

Monti
  • 1
  • I'm using AFNetworking 2.0. Try in class A in viewDidLoad write this:[[AFNetworkReachabilityManager sharedManager] startMonitoring]; Then before you call your class do this:[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); }]; and status it's enum – Сергей Олейнич Jan 21 '15 at 09:19
  • But i am using the AFNetworking version 1.0 – Monti Jan 21 '15 at 09:40

1 Answers1

0
  • (void)startMonitoringWithStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {

    [self stopMonitoring];

    if (!self.networkReachability) { return; }

    __weak __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 flags;

    SCNetworkReachabilityGetFlags(self.networkReachability, &flags);

    dispatch_async(dispatch_get_main_queue(), ^{ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); callback(status); });

    SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);

}

Kuldeep Singh
  • 334
  • 1
  • 11