Guest User

Untitled

a guest
Jan 12th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #import "MyCustomProtocol.h"
  2. #import "Constants.h"
  3.  
  4. @implementation MyCustomProtocol
  5.  
  6. + (BOOL) canInitWithRequest:(NSURLRequest *)request {
  7. NSURL* theURL = request.URL;
  8. NSString* scheme = theURL.scheme;
  9. if([scheme isEqualToString:@"customProtocol"]) {
  10. return YES;
  11. }
  12. return NO;
  13. }
  14.  
  15. // You could modify the request here, but I'm doing my legwork in startLoading
  16. + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
  17. return request;
  18. }
  19.  
  20. // I'm not doing any custom cache work
  21. + (BOOL) requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
  22. return [super requestIsCacheEquivalent:a toRequest:b];
  23. }
  24.  
  25. // This is where I inject my header
  26. // I take the handled request, add a header, and turn it back into http
  27. // Then I fire it off
  28. - (void) startLoading {
  29. NSMutableURLRequest* mutableRequest = [self.request mutableCopy];
  30. Constants *constants = [Constants sharedInstance];
  31. [mutableRequest setValue:[NSString stringWithFormat:@"Bearer %@",constants.access_token] forHTTPHeaderField:@"Authorization"];
  32.  
  33. NSURL* newUrl = [[NSURL alloc] initWithScheme:@"http" host:[mutableRequest.URL host] path:[mutableRequest.URL path]];
  34. [mutableRequest setURL:newUrl];
  35.  
  36. self.connection = [NSURLConnection connectionWithRequest:mutableRequest delegate:self];
  37. }
  38.  
  39. - (void) stopLoading {
  40. [self.connection cancel];
  41. }
  42.  
  43. // Below are boilerplate delegate implementations
  44. // They are responsible for letting our client (the MPMovePlayerController) what happened
  45.  
  46. - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  47. [self.client URLProtocol:self didFailWithError:error];
  48. self.connection = nil;
  49. }
  50.  
  51. - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  52. [self.client URLProtocol:self didLoadData:data];
  53. }
  54.  
  55. - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  56. [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
  57. }
  58.  
  59. - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
  60. [self.client URLProtocolDidFinishLoading:self];
  61. self.connection = nil;
  62. }
  63.  
  64. @end
Add Comment
Please, Sign In to add comment