写在前面
其实简书上这篇文章都总结的很好,我也是参考的这个,只是想简单记录总结下 NSURLProtocol在自己项目使用场景,于是就有了这个小结。
项目中应用场景,即要解决的问题?
app中banner活动链接为加载h5页面,那么问题来了,有的活动是已登录用户才允许操作,例如抽奖。那h5页面怎么识别该用户是已登录还是未登录呢,从而给出相应的提示。(例如,没有登录的话,页面就提示用户马上登录,已经登录的话,就提示某某用户,您好,您还剩几次抽奖机会等等。)
上面的问题,单从技术层面来说,会有很多解决办法,js与oc交互之桥接WebViewJavascriptBridge框架使用就可以解决,当然还有下面要讲的我们项目中现在使用的方法。大家若是用到其它的更好的办法,非常欢迎留言,一起学习,一起进步哈😝
问题大概解决思路?
iOS客户端加载h5页面的过程中,能拦截到所有有关这个链接页面的请求,截取带authentication关键字的url,然后添加相应的参数转发到后台那边即可。
- 拦截到所要处理的请求url:点击banner超链接之后,程序要能拦截到所有的请求,像css请求,ajax请求等等(最主要是能拦截到我们需要更改的那个url)。开始以为很简单,以为这些请求都会走uiwebview的shouldStartLoadWithRequest代理,之后进行拦截更改处理就行,其实不行,像css,ajax请求都不会走这个代理。
- 篡改url转发:当截取到了我们需要的url之后,下面就相对简单了,我们要考虑的就是怎么给这个url加上参数,之后让它跟正常一样的请求了。(这里注意不是重新发送请求)
正题
上面啰嗦了那么多,下面还是针对解决思路分别上代码比较明了。(NSURLProtocol使用)
什么是NSURLProtocol,它是干什么用的呢?
NSURLProtocol是一个挺牛逼的类,它是一个抽象类,不能去实例化它,只能子类化NSURLProtocol,
每次在对一个 URL 进行请求的时候 URL Loading System 都会向 已经注册的 Protocol 询问是否可以处理该请求。这里就看出他的作用来了. 比如: 拦截UIWebView的请求,忽略请求,重定向... ... 引用参考该链接,感谢简友分享
-
拦截url
继承NSURLProtocol的FilteredProtocol子类,h文件为:
#import <Foundation/Foundation.h>
static NSString*const FilteredCssKey = @"filteredCssKey";
@interface FilteredProtocol : NSURLProtocol
@property (nonatomic, strong) NSMutableData *responseData;
@property (nonatomic, strong) NSURLConnection *connection;
+ (BOOL)canInitWithRequest:(NSURLRequest *)request;
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b;
- (void)startLoading;
- (void)stopLoading;
@end```
FilteredProtocol.m代码如下,为方便亲们测试还是直接copy出来吧(有点纠结,这样搞篇幅会好长,有谁知道简书怎么上传代码文件啊)
import "FilteredProtocol.h"
@implementation FilteredProtocol
-
(BOOL)canInitWithRequest:(NSURLRequest *)request
{
// NSLog(@"request.URL.absoluteString = %@",request.URL.absoluteString);
//只处理http和https请求
NSString *scheme = [[request URL] scheme];
if ( ([scheme caseInsensitiveCompare:@"http"] == NSOrderedSame ||
[scheme caseInsensitiveCompare:@"https"] == NSOrderedSame ))
{
//看看是否已经处理过了,防止无限循环
if ([NSURLProtocol propertyForKey:FilteredCssKey inRequest:request])
return NO;return YES;
}
return NO;
} -
(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
//会打印所有的请求链接包括css和ajax请求等
NSLog(@"request.URL.absoluteString = %@",request.URL.absoluteString);NSMutableURLRequest *mutableReqeust = [request mutableCopy];
//截取重定向
if ([request.URL.absoluteString rangeOfString:@"authentication"].length > 0)
{
NSString str = [StaticTools addParaWithOriginUrl:request.URL.absoluteString];
NSURL url1 = [NSURL URLWithString:str];
mutableReqeust = [NSMutableURLRequest requestWithURL:url1];
}
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:FilteredCssKey inRequest:mutableReqeust];BOOL enableDebug = NO;
//这里最好加上缓存判断
if (enableDebug)
{
NSString *str = @"写代码是一门艺术";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:mutableReqeust.URL
MIMEType:@"text/plain"
expectedContentLength:data.length
textEncodingName:nil];
[self.client URLProtocol:self
didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
else
{
self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
}
} (void)stopLoading
{
if (self.connection != nil)
{
[self.connection cancel];
self.connection = nil;
}
}
pragma mark- NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.client URLProtocol:self didFailWithError:error];
}
pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.responseData = [[NSMutableData alloc] init];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}
-
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
[self.client URLProtocol:self didLoadData:data];
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.client URLProtocolDidFinishLoading:self];
}(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
if (response != nil)
{
[[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
}
return request;
}
@end```
FilteredProtocol类写好之后,最后在你uiwebview使用类中,注册FilteredProtocol类,代码如下(考虑到我们项目uiwebview加载相对比较少,性能影响也不会很大,所以我们项目暂时是注册在appdelegate中的)
#import "FilteredProtocol.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//监听webview加载,处理含有authentication的url
[NSURLProtocol registerClass:[FilteredProtocol class]];
return YES;
}
-
篡改url转发
其实篡改url转发的代码,细心的亲们应该已经发现了就在上面,下面再重点贴出来下
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
//会打印所有的请求链接包括css和ajax请求等
NSLog(@"request.URL.absoluteString = %@",request.URL.absoluteString);
NSMutableURLRequest *mutableReqeust = [request mutableCopy];
//截取重定向
if ([request.URL.absoluteString rangeOfString:@"authentication"].length > 0)
{
NSString *str = [StaticTools addParaWithOriginUrl:request.URL.absoluteString];
NSURL* url1 = [NSURL URLWithString:str];
mutableReqeust = [NSMutableURLRequest requestWithURL:url1];
}
return mutableReqeust;
}
到这里,你再运行程序,会发现所有的请求都可以被截取到,并且如你所愿的修改了😝
后记
网上搜索NSURLCache也能实现如上问题,我们项目也尝试用这种解决办法,最终因为某些原因,没能解决如上问题,若有用NSURLCache成功解决者,非常欢迎留言,好好学习下🙏
NSURLCache参考链接