Skip to content

Instantly share code, notes, and snippets.

@joshtwist
Last active December 15, 2015 02:08
Show Gist options
  • Select an option

  • Save joshtwist/5184610 to your computer and use it in GitHub Desktop.

Select an option

Save joshtwist/5184610 to your computer and use it in GitHub Desktop.
Filter to add parameters to each request (to assist with versioning)
- (void)handleRequest:(NSURLRequest *)request
next:(MSFilterNextBlock)next
response:(MSFilterResponseBlock)response
{
// A wrapped response block that decrements the busy counter
MSFilterResponseBlock wrappedResponse = ^(NSHTTPURLResponse *innerResponse, NSData *data, NSError *error) {
response(innerResponse, data, error);
};
// add additional versioning information to the querystring for versioning purposes
NSString *append = [NSString stringWithFormat:@"build=%@&version=%@", self.build, self.version];
NSURL *url = nil;
NSRange range = [request.URL.absoluteString rangeOfString:@"?"];
if (range.length > 0) {
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@&%@&p=iOS", request.URL.absoluteString, append]];
}
else {
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@&p=iOS", request.URL.absoluteString, append]];
}
NSMutableURLRequest *newRequest = [request mutableCopy];
newRequest.URL = url;
next(newRequest, wrappedResponse);
}
// elsewhere in the file
self.version = [self urlEncoded:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
self.build = [self urlEncoded:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
-(NSString *)urlEncoded:(NSString *) input
{
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]%. ",
kCFStringEncodingUTF8 );
return (__bridge NSString *)urlString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment