Question

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

Was it helpful?

Solution

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

OTHER TIPS

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"];

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top