Guest User

Untitled

a guest
Oct 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #import "YappPhoneGapLocalURLProtocol.h"
  2.  
  3. #ifdef PHONEGAP_FRAMEWORK
  4. #import <PhoneGap/PhoneGapDelegate.h>
  5. #else
  6. #import "PhoneGapDelegate.h"
  7. #endif
  8.  
  9. @interface YappPhoneGapLocalURLProtocol () <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
  10. @property (nonatomic, retain) NSURLConnection *connection;
  11. @end
  12.  
  13. @implementation YappPhoneGapLocalURLProtocol
  14.  
  15. @synthesize connection = connection_;
  16.  
  17. -(void)dealloc
  18. {
  19. [self.connection cancel];
  20. self.connection = nil;
  21. [super dealloc];
  22. }
  23.  
  24. + (BOOL)canInitWithRequest:(NSURLRequest *)request
  25. {
  26. NSURL *url = [request URL];
  27. if ([[url scheme] isEqualToString:@"http"] &&
  28. [[url host] isEqualToString:@"phonegap.yapp.us"]) {
  29. return YES;
  30. }
  31. return NO;
  32. }
  33.  
  34. + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
  35. {
  36. return request;
  37. }
  38.  
  39. - (void)startLoading
  40. {
  41. NSLog(@"YappPhoneGapLocalURLProtocol startLoading %@", [self.request URL]);
  42. NSString *path = [[self.request URL] path];
  43. NSString *resourcePath = [PhoneGapDelegate pathForResource:path];
  44. NSURL *resourceURL = [NSURL fileURLWithPath:resourcePath];
  45. NSURLRequest *resouceRequest = [NSURLRequest requestWithURL:resourceURL];
  46. self.connection = [NSURLConnection connectionWithRequest:resouceRequest delegate:self];
  47. }
  48.  
  49. - (void)stopLoading
  50. {
  51. [self.connection cancel];
  52. }
  53.  
  54. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
  55. {
  56. return cachedResponse;
  57. }
  58.  
  59. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
  60. {
  61. return request;
  62. }
  63.  
  64. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  65. {
  66. [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
  67. }
  68.  
  69. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  70. {
  71. [self.client URLProtocol:self didLoadData:data];
  72. }
  73.  
  74. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  75. {
  76. [self.client URLProtocol:self didFailWithError:error];
  77. self.connection = nil;
  78. }
  79.  
  80.  
  81. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  82. {
  83. [self.client URLProtocolDidFinishLoading:self];
  84. self.connection = nil;
  85. }
  86. @end
Add Comment
Please, Sign In to add comment