ios AFNetworking 检查可用性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15041631/
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 22:24:46  来源:igfitidea点击:

AFNetworking checking Availability

iosios6afnetworkingreachability

提问by AMayes

I've implemented AFNetworkingwithout subclassing AFHTTPClient, in part using the following code in my DownloadQueueManager:

我在AFNetworking没有子类化的情况下实现了AFHTTPClient,部分使用了我的以下代码DownloadQueueManager

-(void)downloadPodcastAt:(NSString *)url toPath:(NSString *)path
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:60.0];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        [self saveQueuedItemInformation];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        // Other stuff
    }];

    [operation start];
}

My question is manifold. I've googled til' my fingers went numb, and have yet to find a decent code sample that simply and easily checks for Reachability status using AFNetworking. (Oddly, there is plenty of discussion about importing SystemConfiguration.framework, which seems like a no-brainer). So if my user wants to minimize their data usage, and only download using wifi, how do I check for wifi, and only download if wifi is available?

我的问题是多方面的。我一直在 google 上搜索,直到我的手指麻木了,还没有找到一个像样的代码示例,它可以使用AFNetworking. (奇怪的是,有很多关于 importing 的讨论SystemConfiguration.framework,这似乎很简单)。因此,如果我的用户想要最大限度地减少数据使用量,并且仅使用 wifi 下载,我该如何检查 wifi,并且仅在 wifi 可用时才下载?

Second, it seems like AFNetworkingwants to be a user-friendly front-end. But I could actually use a front-end to this front-end, because there's a LOT of stuff in there that one has to weed through to get to the stuff one needs. I just need to access a url, download an xml file (based on reachability), and do stuff with it. Am I missing something that makes this a simple task ?

其次,它似乎AFNetworking想成为一个用户友好的前端。但我实际上可以使用前端到这个前端,因为那里有很多东西,人们必须清除才能获得需要的东西。我只需要访问一个 url,下载一个 xml 文件(基于可达性),然后用它做一些事情。我是否错过了使这成为一项简单任务的东西?

When I make sense of this, I'm totally building a front-end or five to simplify implementation (assuming I'm not just an idiot). Thanks in advance for any responses.

当我理解这一点时,我完全是在构建一个或五个前端来简化实现(假设我不仅仅是一个白痴)。提前感谢您的任何答复。

回答by Keith Smiley

Actually contrary to what A-Live saidReachability IS a part of AFNetworking. It's implemented in AFHTTPClient.hhere. You need the correct imports in your .pchfile as discussed herein order to use it.

实际上与A-Live 所说的相反,可达性是 AFNetworking 的一部分。它在AFHTTPClient.h这里实现。您需要正确的进口你的.pch文件作为讨论这里以使用它。

To use it you'll probably want to have a subclass of AFHTTPClientso you can use setReachabilityStatusChangeBlockdefined here. Here's a simple example without using a subclass.

要使用它,您可能需要一个子类,AFHTTPClient以便您可以在此处使用setReachabilityStatusChangeBlock定义。这是一个不使用子类的简单示例。

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://google.com"]];
[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if (status == AFNetworkReachabilityStatusNotReachable) {
        // Not reachable
    } else {
        // Reachable
    }

    if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
        // On wifi
    }
}];

If you don't like how this reachability setup works then I would recommend Tony Million's forkof Apple's Reachability. Simple example:

如果您不喜欢这种可达性设置的工作方式,那么我会推荐Tony Million 的Apple 可达性分支。简单的例子:

Reachability *reach = [Reachability reachabilityWithHostname:@"google.com"];
if ([reach isReachable]) {
    // Reachable
    if ([reach isReachableViaWiFi]) {
        // On WiFi
    }
} else {
    // Isn't reachable

    [reach setReachableBlock:^(Reachability *reachblock)
    {
        // Now reachable
    }];

    [reach setUnreachableBlock:^(Reachability*reach)
    {
        // Now unreachable
    }];
}

回答by Hemang

With AFNetworking2.0 and above, one can check for availability like this,

使用AFNetworking2.0 及更高版本,可以像这样检查可用性,

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            //available
            break;
        case AFNetworkReachabilityStatusNotReachable:
            //not available
            break;
        default:
            break;
    }

    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));

}];

//start monitoring
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

To get current status

获取当前状态

[AFNetworkReachabilityManager sharedManager].reachable

回答by Shawn Frank

Just an update, the newer version of AFNetworking has deprecated AFHTTPClient.

只是更新,较新版本的 AFNetworking 已弃用 AFHTTPClient。

You can use AFHTTPRequestOperationManager.h instead

您可以改用 AFHTTPRequestOperationManager.h

Something small taken from the github page itself:

取自 github 页面本身的一些小东西:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; //url can be google.com or something you want to reach

NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)

{
    switch (status)
    {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
        {
            NSLog(@"SO REACHABLE");
            [operationQueue setSuspended:NO]; // or do whatever you want
            break;
        }

        case AFNetworkReachabilityStatusNotReachable:
        default:
        {
            NSLog(@"SO UNREACHABLE");
            [operationQueue setSuspended:YES]; 
            //not reachable,inform user perhaps
            break;
        }
    }
}];
[manager.reachabilityManager startMonitoring];