-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAllowedUrlPrefixProtocol.mm
40 lines (34 loc) · 1.42 KB
/
AllowedUrlPrefixProtocol.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#import "AllowedUrlPrefixProtocol.h"
@implementation AllowedUrlPrefixProtocol
NSArray *allowedUrlPrefixes = nil;
+ (NSArray *)getAllowedUrlPrefixes
{
if (allowedUrlPrefixes == nil) {
NSString *allowedUrlPrefixList = [[NSBundle mainBundle].infoDictionary objectForKey:@"AllowedUrlPrefixes"];
allowedUrlPrefixes = [allowedUrlPrefixList componentsSeparatedByString:@","];
}
return allowedUrlPrefixes;
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSArray *allowedUrlPrefixes = [self getAllowedUrlPrefixes];
BOOL hasPrefixInArray = NO;
for (int i = 0; i < [allowedUrlPrefixes count] && hasPrefixInArray == NO; i++)
{
hasPrefixInArray = [[request.URL.absoluteString lowercaseString] hasPrefix:[allowedUrlPrefixes[i] lowercaseString]];
}
return !hasPrefixInArray;
}
+ (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:@"URL not allowed"}];
[client URLProtocol:self didFailWithError:error];
}
- (void)stopLoading { }
@end