iOS 关于网络重定向的实现以及相关坑的记录

iOS 关于网络重定向的实现以及相关坑的记录

之前在项目中遇到个问题,既可以说是坑也可以说是自己对一些非常iOS特性的东西的不了解所导致。记录以备有需要的同学或者自己忘了可以回头看看。

先说下简化后的场景:现在server1提供了一个url让我去打开它,然后用户就在这个html的网页上做操作,然后比如点击某个按键提交一些信息到server1,问题来了,这个时候server1与另一个server2做交互并对url进行多次重定向。。具体更复杂一点,大概就是原来用某个url显示的webview经过url的重定向,而我需要拿到每次重定向的response,这里就出现了最后一个response我拿不到的情形。当然每次的request我可以拿到,那么request的url我也可以拿到,那我手动去请求,但这又不符合应用场景,而且也不太科学。

开始折腾了好一会,也很郁闷,后来发现类似这种情况是必须自己手动去实现NSURLProtocol:

1
2
3
4
@interface RedirectURLProtocol : NSURLProtocol
{
}
@end
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#import "RedirectURLProtocol.h"
static NSString * const URLProtocolHandledKey = @"URLProtocolHandledKey";
@interface RedirectURLProtocol ()<NSURLConnectionDelegate>
@property (nonatomic, strong) NSURLConnection *connection;
@end
@implementation RedirectURLProtocol
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
//只处理http和https请求
NSString *scheme = [[request URL] scheme];
if ( ([scheme caseInsensitiveCompare:@"http"] == NSOrderedSame ||
[scheme caseInsensitiveCompare:@"https"] == NSOrderedSame))
{
//看看是否已经处理过了,防止无限循环
if ([NSURLProtocol propertyForKey:URLProtocolHandledKey inRequest:request]) {
return NO;
}
return YES;
}
return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
NSMutableURLRequest *mutableReqeust = [request mutableCopy];
mutableReqeust = [self redirectHostInRequset:mutableReqeust];
return mutableReqeust;
}
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b
{
return [super requestIsCacheEquivalent:a toRequest:b];
}
- (void)startLoading
{
NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
//打标签,防止无限循环
[NSURLProtocol setProperty:@YES forKey:URLProtocolHandledKey inRequest:mutableReqeust];
self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
}
- (void)stopLoading
{
[self.connection cancel];
}
#pragma mark - NSURLConnectionDelegate
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
NSHTTPURLResponse* tempResponse = (NSHTTPURLResponse*)response;
NSLog(@"============ header = %@", tempResponse.allHeaderFields.allKeys);
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.client URLProtocol:self didLoadData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[self.client URLProtocolDidFinishLoading:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.client URLProtocol:self didFailWithError:error];
}
#pragma mark -- private
+(NSMutableURLRequest*)redirectHostInRequset:(NSMutableURLRequest*)request
{
if ([request.URL host].length == 0) {
return request;
}
NSString *originUrlString = [request.URL absoluteString];
NSString *originHostString = [request.URL host];
NSRange hostRange = [originUrlString rangeOfString:originHostString];
if (hostRange.location == NSNotFound) {
return request;
}
// 替换url,现有不需要去重定向不同的url,所以直接将request的urlcopy
NSString *urlString = request.URL.absoluteString;
NSURL *url = [NSURL URLWithString:urlString];
request.URL = url;
return request;
}
@end

这样子我就将会在- (void) connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse )response这个函数里获得response
在你需要之前加入以下这句话

1
[NSURLProtocol registerClass:[RedirectURLProtocol class]];

用完记得还是把它移除掉,不然你的重定向协议是会一直影响你的App的,直到你自己手动移除掉它。

1
[NSURLProtocol unregisterClass:[RedirectURLProtocol class]];

先记录到这,其实这样子你就可以自己去定制协议定制规则,but最好别这么干吧,需要点功底的感觉。不然自己坑自己的节奏~