测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可 | MIT |
发布上次发布 | 2015年2月 |
由Marcus Westin、Marcus Westin、Marcus Westin维护。
为web视图代理请求,容易且无需与NSURLProtocol纠缠。
适用于iOS和OSX。
对拦截请求的响应可以是同步的或异步的 - 这与UIWebViewDelegate
方法-(NSCachedURLResponse *)cachedResponseForRequest:url:host:path:
形成对比,该方法只能同步拦截请求和响应(这使得无法在例如不阻塞网络请求的情况下通过网络代理请求)。
如果您喜欢WebViewProxy,还应该查看WebViewJavascriptBridge。
拦截所有具有给定方案的UIWebView请求。
示例
[WebViewProxy handleRequestsWithScheme:@"my_custom_scheme" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
拦截所有具有给定主机的UIWebView请求。
示例
[WebViewProxy handleRequestsWithHost:@"foo" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
拦截所有匹配给定主机和URL路径的UIWebView请求。
示例
[WebViewProxy handleRequestsWithHost:@"foo.com" path:@"/bar" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
拦截所有匹配给定主机和URL路径前缀的UIWebView请求。
例如,使用[WebViewProxy handleRequestsWithHost:@"foo.com" pathPrefix:@"/bar" handler:...]
注册的处理器将拦截对http://foo.com/bar
、https://foo.com/bar/cat?wee=yes
、http://foo.com/bar/arbitrarily/long/subpath
等请求的请求。
示例
[WebViewProxy handleRequestsWithHost:@"foo.com" pathPrefix:@"/bar" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
拦截所有满足给定NSPredicate
的NSURL
的UIWebView请求。
示例
[WebViewProxy handleRequestsMatching:[NSPredicate predicateWithFormat:@"absoluteString MATCHES[cd] '^http:'"] handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
[WebViewProxy handleRequestsMatching:[NSPredicate predicateWithFormat:@"host MATCHES[cd] '[foo|bar]'"] handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
所有注册的处理器都获得一个WVPRespone* res
。您通过调用此对象的方法对请求进行响应。
响应请求有3种类型的API
NSURLConnection
通过WVPResponse
传递描述和示例将详细说明。
以图像响应(默认使用Content-Type "image/png",对于以.jpg
或.jpeg
结尾的请求,使用"image/jpg")
示例
[WebViewProxy handleRequestsWithHost:@"imageExample" path:@"GoogleLogo.png" handler:^(NSURLRequest* req, WVPResponse *res) {
UIImage* image = [UIImage imageNamed:@"GoogleLogo.png"];
[res respondWithImage:image];
}];
以图像和给定的MIME类型响应。
示例
[WebViewProxy handleRequestsWithHost:@"imageExample" handler:^(NSURLRequest* req, WVPResponse *res) {
UIImage* image = [UIImage imageNamed:@"GoogleLogo.png"];
[res respondWithImage:image mimeType:@"image/png"];
}];
以文本形式响应(默认使用Content-Type "text/plain")
[WebViewProxy handleRequestsWithHost:@"textExample" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"Hi!"];
}];
以HTML形式响应(默认使用Content-Type "text/html")
[WebViewProxy handleRequestsWithHost:@"htmlExample" handler:^(NSURLRequest* req, WVPResponse *res) {
[res respondWithText:@"<div class='notification'>Hi!</div>"];
}];
以JSON形式响应(默认使用Content-Type "application/json")
[WebViewProxy handleRequestsWithHost:@"textExample" handler:^(NSURLRequest* req, WVPResponse *res) {
NSDictionary* jsonObject = [NSDictionary dictionaryWithObject:@"foo" forKey:@"bar"];
[res respondWithJSON:jsonObject]; // sends '{ "bar":"foo" }'
}];
描述和示例将详细说明。
以指定的HTTP状态代码和文本响应。
示例
[res respondWithStatusCode:400 text:@"Bad request"];
[res respondWithStatusCode:404 text:@"Not found"];
在响应前设置响应头。
示例
[res setHeader:@"Content-Type" value:@"image/gif"];
[res setHeader:@"Content-Type" value:@"audio/wav"];
[res setHeader:@"Host" value:@"WebViewProxy"];
在响应前设置多个响应头。
示例
[res setHeaders:@{ @"Content-Type":@"image/gif", @"Host":@"WebViewProxy" }];
以给定数据和MIME类型响应(MIME类型作为HTTP头Content-Type
发送)。
如果mimeType为nil,则WebWiewProxy将尝试从请求URL路径扩展名推断它。
示例
NSString* greeting = @"Hi!";
NSData* data = [greeting dataUsingEncoding:NSUTF8StringEncoding];
[res respondWithData:data mimeType:@"text/plain"];
以给定数据、MIME类型和HTTP状态代码响应(MIME类型作为HTTP头Content-Type
发送)。
如果mimeType为nil,则WebWiewProxy将尝试从请求URL路径扩展名推断它。
示例
NSData* data = [@"<div>Item has been created</div>" dataUsingEncoding:NSUTF8StringEncoding];
[res respondWithData:data mimeType:@"text/html" statusCode:201];
[res respondWithData:nil mimeType:nil statusCode:304]; // HTTP status code 304 "Not modified"
[res respondWithData:nil mimeType:nil statusCode:204]; // HTTP status code 204 "No Content"
响应的缓存策略。默认值是NSURLCacheStorageNotAllowed
示例
response.cachePolicy = NSURLCacheStorageAllowed;
response.cachePolicy = NSURLCacheStorageAllowedInMemoryOnly;
response.cachePolicy = NSURLCacheStorageNotAllowed;
有几种方法可以使用WebViewProxy
代理远程请求。
最简单的方法是将WebViewProxyResponse
作为NSURLConnection
的代理。这将通过代理响应管道响应
[WebViewProxy handleRequestsWithHost:@"example.proxy" handler:^(NSURLRequest *req, WVPResponse *res) {
NSString* proxyUrl = [req.URL.absoluteString stringByReplacingOccurrencesOfString:@"example.proxy" withString:@"example.com"];
NSURLRequest* proxyReq = [NSURLRequest requestWithURL:[NSURL URLWithString:proxyUrl]];
[NSURLConnection connectionWithRequest:proxyReq delegate:res];
}];
另一种方法可以提供更多控制,但会将整个响应读入内存中
[WebViewProxy handleRequestsWithHost:@"example.proxy" handler:^(NSURLRequest *req, WVPResponse *res) {
NSString* proxyUrl = [req.URL.absoluteString stringByReplacingOccurrencesOfString:@"example.proxy" withString:@"example.com"];
NSURLRequest* proxyReq = [NSURLRequest requestWithURL:[NSURL URLWithString:proxyUrl]];
NSOperationQueue* queue = [NSOperationQueue new];
[NSURLConnection sendAsynchronousRequest:proxyReq queue:queue completionHandler:^(NSURLResponse* proxyRes, NSData* proxyData, NSError* proxyErr) {
if (proxyErr) {
return [res pipeError:proxyErr];
} else {
NSInteger statusCode = [(NSHTTPURLResponse*)proxyRes statusCode];
[res setHeaders:[(NSHTTPURLResponse*)proxyRes allHeaderFields]];
[res respondWithData:proxyData mimeType:proxyRes.MIMEType statusCode:statusCode];
}
}];
}];
将一个 NSURLResponse
及其数据传入 WVPResponse
。这使得通过 NSURLConnection 代理请求及其响应变得简单。
待编写示例。
将一个 NSURLResponse 传入响应。
将数据传入响应。
将错误传入响应(例如:网络错误)。
完成管道响应。
请求处理器可能会被告知“停止加载”。这可能会发生在,例如在底层的 NSURLRequest
上调用 cancel:
的结果。您可以通过 WVPResponse
上的 handleStopLoadingRequest:
方法获取通知。
此 API 可以用于停止在请求处理器中执行昂贵计算等操作。
示例
[WebViewProxy handleRequestsMatching:predicate handler:^(NSURLRequest* req, WVPResponse *res) {
NSOperation* expensiveOperation = [self startExpensiveOperation];
[res handleStopLoadingRequest:^{
[expensiveOperation cancel]
}];
}];