/
AppDelegate.m
101 lines (80 loc) · 5.24 KB
/
AppDelegate.m
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
99
100
101
#import "AppDelegate.h"
#import "WebViewProxy.h"
@implementation AppDelegate
static NSOperationQueue* queue;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:5];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[WebViewProxy handleRequestsWithHost:@"www.google.com" path:@"/images/srpr/logo3w.png" handler:^(NSURLRequest* req, WVPResponse *res) {
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:req.URL] queue:queue completionHandler:^(NSURLResponse *netRes, NSData *data, NSError *netErr) {
if (netErr || ((NSHTTPURLResponse*)netRes).statusCode >= 400) { return [res respondWithError:500 text:@":("]; }
[res respondWithData:data mimeType:@"image/png"];
}];
}];
[WebViewProxy handleRequestsWithHost:@"black_and_white.google.com" path:@"/images/srpr/logo3w.png" handler:^(NSURLRequest* req, WVPResponse *res) {
NSURL* url = [NSURL URLWithString:[req.URL.absoluteString stringByReplacingOccurrencesOfString:@"black_and_white" withString:@"www"]];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:queue completionHandler:^(NSURLResponse *netRes, NSData *netData, NSError *netErr) {
if (netErr || ((NSHTTPURLResponse*)netRes).statusCode >= 400) { return [res respondWithError:500 text:@":("]; }
UIImage* originalImage = [UIImage imageWithData:netData];
[res respondWithImage:[self blackAndWhite:originalImage]];
}];
}];
[WebViewProxy handleRequestsWithHost:@"intercept" path:@"/Galaxy.png" handler:^(NSURLRequest* req, WVPResponse *res) {
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Galaxy" ofType:@"png"];
UIImage* image = [UIImage imageWithContentsOfFile:filePath];
[res respondWithImage:image];
}];
[WebViewProxy handleRequestsWithHost:@"intercept_black_and_white" path:@"/Galaxy.png" handler:^(NSURLRequest* req, WVPResponse *res) {
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Galaxy.png" ofType:nil];
UIImage* originalImage = [UIImage imageWithContentsOfFile:filePath];
[res respondWithImage:[self blackAndWhite:originalImage]];
}];
UIWebView* webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString* webViewContentPath = [[NSBundle mainBundle] pathForResource:@"WebViewContent" ofType:@"html"];
NSString* htmlContent = [NSString stringWithContentsOfFile:webViewContentPath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlContent baseURL:nil];
[self.window addSubview:webView];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[UIViewController alloc] init]; // "Application windows are expected to have a root view controller at the end of application launch"... why? So silly.
[self.window makeKeyAndVisible];
// More examples that won't do anything in this app
[WebViewProxy handleRequestsWithScheme:@"my_custom_scheme" handler:^(NSURLRequest* req, WVPResponse *res) {
// ...
}];
[WebViewProxy handleRequestsWithHost:@"foo.com" handler:^(NSURLRequest* req, WVPResponse *res) {
// ...
}];
[WebViewProxy handleRequestsWithHost:@"foo.com" path:@"/bar" handler:^(NSURLRequest* req, WVPResponse *res) {
// ...
}];
[WebViewProxy handleRequestsWithHost:@"foo.com" pathPrefix:@"/bar" handler:^(NSURLRequest* req, WVPResponse *res) {
// ...
}];
[WebViewProxy handleRequestsMatching:[NSPredicate predicateWithFormat:@"absoluteString MATCHES[cd] '^http:'"] handler:^(NSURLRequest* req, WVPResponse *res) {
// ...
}];
[WebViewProxy handleRequestsMatching:[NSPredicate predicateWithFormat:@"host MATCHES[cd] '[foo|bar]'"] handler:^(NSURLRequest* req, WVPResponse *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]];
[NSURLConnection connectionWithRequest:proxyReq delegate:res];
}];
return YES;
}
- (UIImage *)blackAndWhite:(UIImage *)originalImage {
CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width, colorSapce, kCGImageAlphaNone);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);
CGImageRef bwImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSapce);
UIImage *resultImage = [UIImage imageWithCGImage:bwImage]; // This is result B/W image.
CGImageRelease(bwImage);
return resultImage;
}
@end