ios AFNetworking 和无 Internet 连接方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9253496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:48:36  来源:igfitidea点击:

AFNetworking and No Internet Connection scenario

iphoneioscocoa-touchafnetworking

提问by Fred Collins

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

AFNetworking在我的应用程序中使用每个请求(如登录、从 url 获取数据等)。

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

以这个为例:用户点击登录按钮并且没有连接,如何立即显示一个UIAlertView表示错误的信息?该唯一的办法就是等待请求超时并执行failure块?有没有一种方法可以立即检查是否有连接?

Thanks!

谢谢!

回答by mattt

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

从 0.9 开始,AFHTTPClient实际上内置了网络可达性(Apple 前面提到的可达性代码的更简单的接口)。只需包含SystemConfiguration框架并用于-setReachabilityStatusChangeBlock:在可达性状态更改时指定响应。

回答by Yogesh Agarwal

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

随着AFNetworking这些步骤一个是为了趁跟随setReachabilityStatusChangeBlock:加入AFNetworing班后-

  1. Add SystemConfiguration.frameworkto your project
  2. In pch file add #import <SystemConfiguration/SystemConfiguration.h>
  3. Assuming that you have a subclass of AFHTTPClientin this subclass add below lines of code in init function -
  1. 添加SystemConfiguration.framework到您的项目
  2. 在 pch 文件中添加 #import <SystemConfiguration/SystemConfiguration.h>
  3. 假设您AFHTTPClient在此子类中有一个子类,请在 init 函数中添加以下代码行 -
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"changed %d", status);
        //your code here
    }];

回答by carmen_munich

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

我使用AFNetworkingOperationDidFinishNotification. 每次http请求失败时,都会弹出alert并通知用户

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

回答by bs7

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

也许您可以使用“可达性”来确定设备是否已连接到网络。这是 Apple 文档的链接。:可达性

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
}

回答by Adam Eberbach

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 Reachabilityshows how to get an initial status.

看起来像Apple Sample Project for Reachability展示了如何获得初始状态。