I need to modify response headers in an NSURLResponse. Is this possible?
I was just talking about this with a friend. My suggestion would be to write a subclass of NSURLResponse. Something along these lines:
@interface MyHTTPURLResponse : NSURLResponse { NSDictionary *myDict; }
- (void)setAllHeaderFields:(NSDictionary *)dictionary;
@end
@implementation MyHTTPURLResponse
- (NSDictionary *)allHeaderFields { return myDict ?: [super allHeaderFields]; }
- (void)setAllHeaderFields:(NSDictionary *)dict { if (myDict != dict) { [myDict release]; myDict = [dict retain]; } }
@end
If you're dealing with an object you didn't make, you can try using object_setClass to swizzle the class out. However I don't know if that will add the necessary instance variable. You could also use objc_setAssociatedObject and stuff this all in a category instead, if you can support a new enough SDK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With