Last active
December 25, 2016 08:47
-
-
Save saiday/61f138b17e68938906f49e67dc5b708b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ImnotyoursonURLProtocol.h" | |
#import "User.h" | |
#import "Article.h" | |
#import "GenericItem.h" | |
@implementation ImnotyoursonURLProtocol | |
+ (BOOL)canInitWithRequest:(NSURLRequest *)request | |
{ | |
return [request.URL.scheme isEqualToString:@"imnotyourson"]; | |
} | |
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request | |
{ | |
return request; | |
} | |
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b | |
{ | |
return NO; | |
} | |
- (void)startLoading | |
{ | |
// imnotyourson://user/123, imnotyourson://article/123 | |
NSString *path = [[[self.request.URL absoluteString] componentsSeparatedByString:@"//"] lastObject]; | |
GenericItem *item; | |
NSArray *pathArray = [path componentsSeparatedByString:@"/"]; | |
NSString *type = [pathArray firstObject]; | |
NSString *id = [pathArray lastObject]; | |
if ([type isEqualToString:@"article"]) { | |
item = [[Article alloc] init]; | |
item.type = @"article"; | |
} else if ([type isEqualToString:@"user"]) { | |
item = [[User alloc] init]; | |
item.type = @"user"; | |
} | |
item.id = id; | |
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:item]; | |
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.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]; | |
} | |
- (void)stopLoading | |
{ | |
// start loading is sync operation, there's no way to stop | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment