0

I am looking at NSURLProtocol and am trying to insert some headers.

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:kAccessCodeProtocolKey inRequest:newRequest];
    self.connection = [NSURLConnection connectionWithRequest:self.request delegate:self];
}

But my startLoading is never called

jimijon
  • 2,046
  • 1
  • 20
  • 39

3 Answers3

1

I have a feeling you haven't gone threw a deep search, anyway to add a header use the following snippet

NSURL *URL = [NSURL URLWithString:@"http://example.com/..."];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:30.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
streem
  • 9,044
  • 5
  • 30
  • 41
1

Have you implemented canInitWithRequest: ? Does it return YES? If not, startLoading will never be called.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

You would normally initialize it with an NSURL instance and NSMutableURLRequest will provide you the

- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field

Method so you can set your headers in this way

David Berry
  • 40,941
  • 12
  • 84
  • 95
darren102
  • 2,830
  • 1
  • 22
  • 24