Skip to content

Instantly share code, notes, and snippets.

@JunKikuchi
Created June 26, 2012 12:17
Show Gist options
  • Save JunKikuchi/2995517 to your computer and use it in GitHub Desktop.
Save JunKikuchi/2995517 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface MyURLProtocol : NSURLProtocol
+(BOOL)canInitWithRequest:(NSURLRequest *)request;
+(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;
-(void)startLoading;
-(void)stopLoading;
@end
#import "MyURLProtocol.h"
@implementation MyURLProtocol
+(BOOL)canInitWithRequest:(NSURLRequest *)request {
if ([[request.URL host] isEqualToString:@"m.yahoo.co.jp"])
return NO;
NSLog(@"canInitWithRequest: %@", request);
return YES;
}
+(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
-(void)startLoading {
NSLog(@"startLoading");
NSLog(@"requested URL: %@", self.request.URL);
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:[[NSDictionary alloc] init]];
NSData *data = [@"foobar" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = self.request.mutableCopy;
request.URL = [NSURL URLWithString:@"http://m.yahoo.co.jp/"];
NSLog(@"HTTPMethod: %@", [request HTTPMethod]);
NSLog(@"URL: %@", [request URL]);
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.yahoo.co.jp/"]];
[self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
-(void)stopLoading {
NSLog(@"stopLoading");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment