AFNetworking and No Internet Connection scenario

人走茶凉 提交于 2019-12-17 17:32:00

问题


I use AFNetworking in my app for every request (like login, get data from url, etc).

Take this for example: an user click on the login button and there's no connection, how to instantly display a UIAlertView that says the error? The only way is to wait the request timeout and execute the failure block? Isn't there a way that instantly check if there's connection or not?

Thanks!


回答1:


As of 0.9, AFHTTPClient actually has network reachability built-in (a simpler interface to Apple's aforementioned Reachability code). Just include the SystemConfiguration framework and use -setReachabilityStatusChangeBlock: to specify a response when the reachability state changes.




回答2:


With AFNetworking these are the steps that one has to follow in order to take advantage of setReachabilityStatusChangeBlock: after adding the AFNetworing classes -

  1. Add SystemConfiguration.framework to your project
  2. In pch file add #import <SystemConfiguration/SystemConfiguration.h>
  3. Assuming that you have a subclass of AFHTTPClient in this subclass add below lines of code in init function -
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"changed %d", status);
        //your code here
    }];



回答3:


Maybe you could use "Reachability" to determine if the device is connected to the network. Here is the link to the Apple Doc. : Reachability

For example :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
  //Your UIAlertView
}



回答4:


I use the AFNetworkingOperationDidFinishNotification. Every time a http request will fail, the alert pops up and informs the user

- (void)addNetworkObserver
{
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(HTTPOperationDidFinish:) 
                                                name:AFNetworkingOperationDidFinishNotification 
                                              object:nil];
}

- (void)HTTPOperationDidFinish:(NSNotification *)notification 
{
   AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object];
   if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
       return;
   }
   if (operation.error) {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection error"
                                                       message:@"Missing connection to the internet"
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];

       [alert show];
   }
}



回答5:


How about using Reachability?

You can check whether you have a plausible reason for trying a connection before you do it.

Looks like the Apple Sample Project for Reachability shows how to get an initial status.



来源:https://stackoverflow.com/questions/9253496/afnetworking-and-no-internet-connection-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!