CLM logo Development Blog by CLM

Notes on using NSURLRequest

There are a few cases where NSURLRequest becomes handy. For example, we are using it to resolve redirects for URLs and decide where should we open the resolved URL (ie. inside App, Safari, AppStore). It’s possible to make a subdomain like http://review-game.yourdomain.com which redirects users to the AppStore. In my opinion this is a good separation between action and custom URLs — in case that Apple changes anything in review URLs, we can just change URL redirection on server and it works without the App update.

Review URL is a tricky example, because there is a direct link to the App review form for iPhone/iPod, but for iPad there is no such thing and we need to redirect user to App page. We solved it with a simple PHP script:

<?php
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$iPad = strstr($userAgent, "iPad") ? true : false;
$iPodURL = "http://custom-link-for-ipod.com";
$iPadURL = "http://custom-link-for-ipad.com";
$URL = $iPad ? $iPadURL : $iPodURL;

header("Location: $URL");
exit;
?>

There is only one catch — there is no device information included in default NSURLRequest user agent! Default user agent is AppName/Version CFNetwork/485.13.9 Darwin/10.7.0 (versions may vary).

Fortunately it’s very simple to change it, but instead of NSURLRequest we need to use NSMutableURLRequest. Then it’s possible to change userAgent:

NSString* userAgent = @"My custom user agent";
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

We decided to include App name, version and device information and that’s currently enough.

No comments yet

Leave a reply