Sunday 24 April 2016

iOS Network compression gzip

Most of the server enabled gzip compression on demand as header value contain Accept-Encoding:gzip. Now your boss ask that prove me that your app send this request as gzip, then you surf some  articles on internet that says NSURLConnection already send this header, don't need to send it externally. But your boss ask prove me that you got the compressed data, and you cannot find a way linear way to prove that, because NSURLConnection always decompress the data. 
Now you want to see that server send the compressed gzip data and NSURLConnection decompress for you. 

Then use below code for all explanations...



- (void)networkGzipTest {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.co.in"]];
    //    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    
    CFURLRequestRef requestRef = (__bridge CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
    _CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);
    
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [urlConnection start];
}

Actually these two lines make you happy
CFURLRequestRef requestRef = (__bridge CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
_CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);

Now you can print the size of your data without using above two line and with two lines. And all explanation you have now. [Read more]
Happy coding....Voila!

No comments:

Post a Comment