Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutable NSHTTPURLResponse or NSURLResponse

I need to modify response headers in an NSURLResponse. Is this possible?

like image 333
Arlen Anderson Avatar asked May 17 '26 17:05

Arlen Anderson


1 Answers

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.

like image 107
Colin Barrett Avatar answered May 20 '26 12:05

Colin Barrett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!