AFNetworking and No Internet Connection scenario

后端 未结 5 1524
误落风尘
误落风尘 2020-12-07 17:21

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\

相关标签:
5条回答
  • 2020-12-07 17:25

    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.

    0 讨论(0)
  • 2020-12-07 17:31

    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
    }
    
    0 讨论(0)
  • 2020-12-07 17:34

    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];
       }
    }
    
    0 讨论(0)
  • 2020-12-07 17:38

    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.

    0 讨论(0)
  • 2020-12-07 17:50

    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
        }];
    
    0 讨论(0)
提交回复
热议问题