我可以阻止特定时刻的网络访问吗? [英] Can I block network access for a specific moment?

查看:93
本文介绍了我可以阻止特定时刻的网络访问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写iOS应用程序,我会向用户提供阻止此应用程序的网络访问权限的选项。
是否有可能在代码中执行此操作?

Writing an iOS app, I neet to give the user the option to block the network access for this app only. Is it possible doing that in code?

意味着每个调用由代码的任何部分(以及包括静态库)构成都应该被阻止特殊时刻。

Meaning every call, made by whatever part of the code (and that's including static libraries) should be blocked for that particular moment.

推荐答案

您可以使用将拦截的自定义 NSURLProtocol 所有的网络电话。

You can use a custom NSURLProtocol that will intercept all your network calls.

这正是我在所做的工作 OHHTTPStubs 库到存根网络请求(我的库使用私有API来模拟网络响应,但在你的情况下,如果你不需要伪造一个响应您可以避免这些私有API调用并在生产代码中使用此技术)

That's exactly what I do in my OHHTTPStubs library to stub network requests (my library use then private API to simulate a network response, but in your case if you don't need to fake a response you can avoid these calls to the private API and use this technique in production code)


以这个答案, OHHTTPStubs 已经更新,不再使用任何私有API,因此您甚至可以在生产代码中使用它。有关代码示例,请参阅本答案末尾的下面的编辑。

Since this answer, OHHTTPStubs has been updated and don't use any private API anymore, so you can use it even in production code. See my EDIT below at the end of this answer for some code example.







@interface BlockAllRequestsProtocol : NSURLProtocol
@end

@implementation BlockAllRequestsProtocol
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    return YES; // Intercept all outgoing requests, whatever the URL scheme
    // (you can adapt this at your convenience of course if you need to block only specific requests)
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; }
- (NSCachedURLResponse *)cachedResponse { return nil; }

- (void)startLoading
{
    // For every request, emit "didFailWithError:" with an NSError to reflect the network blocking state
    id<NSURLProtocolClient> client = [self client];
    NSError* error = [NSError errorWithDomain:NSURLErrorDomain
                                         code:kCFURLErrorNotConnectedToInternet // = -1009 = error code when network is down
                                     userInfo:@{ NSLocalizedDescriptionKey:@"All network requests are blocked by the application"}];
    [client URLProtocol:self didFailWithError:error];
}
- (void)stopLoading { }

@end

然后安装此协议并阻止所有网络请求:

And then to install this protocol and block all your network requests:

[NSURLProtocol registerClass:[BlockAllRequestsProtocol class]];

稍后将其卸载并让您的网络请求进入现实世界:

And to later uninstall it and let your network requests hit the real world:

[NSURLProtocol unregisterClass:[BlockAllRequestsProtocol class]];






由于我的回答,我已经更新了我的库,它不再使用任何私有API。所以任何人都可以直接使用 OHHTTPStubs ,即使是您需要的用法,例如:

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest* request) {
    return YES; // In your case, you want to prevent ALL requests to hit the real world
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest* request) {
    NSError* noNetworkError = [NSError errorWithDomain:NSURLErrorDomain
                    code:kCFURLErrorNotConnectedToInternet userInfo:nil];
    return [OHHTTPStubsResponse responseWithError:noNetworkError];
}];

这篇关于我可以阻止特定时刻的网络访问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆