Skip navigation.

Harold's Home

XML version of this site

PHP Scripts

CLI fun
Mail on 404
HB-NS (NewsScript)


APOD to Desktop
Dreamweaver Extensions


Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Sample Movies


Set a custom UserAgent in WebKit
A while ago I wrote about my frustration with setting a custom user-agent in Cocoa. Luckily I know people who are a lot smarter than me and an example was provided. I've expanded this a bit and present a codesnippet here for your amusement. There's also precious little on the web at this moment about WebKit so perhaps this is of use to someone else.

The code:

1 NSString *currVersionNumber = [[[NSBundle bundleForClass:[self class]] infoDictionary] objectForKey:@"CFBundleVersion"];
2 NSLog(@"DEBUG - versionnumber: %@", currVersionNumber);
3 NSString *UserAgentString = [NSString stringWithFormat:@"VirtualPetRock/%@ (Macintosh; U; PPC; Mac OS X) AppleWebKit", currVersionNumber];
4 NSLog(@"DEBUG - UserAgentString: %@", UserAgentString);
5 [webStatus setCustomUserAgent:UserAgentString];
6 [[webStatus mainFrame] loadRequest:[NSURLRequest requestWithURL:rssURLMain]];

The explanation:

1 // standard way to get your app's versionnumber
2 // print versionnumber to the console for debug purposes
3 // create a custom UserAgent string, using the versionnumber so we can later analyse our logfiles for adoption rates, replace VirtualPetRock with your own app's name, I've followed the convention of regular browsers and appended AppleWebKit to denote the rendering engine, note that this UserAgent will not be recognised by stupid javascript browsersniffers
4 // print our custom UserAgent to the console for debug purposes
5 // set the darned UserAgent already, note: webStatus is the window in which the loading takes place (we've defined it in AppController.h: IBOutlet WebView *webStatus; and we also made a window named webStatus in InterfaceBuilder)
6 // NOW we can load the requested URL, rssURLMain is a string we've constructed earlier and is in effect the url one would type in the locationbar

Show all items | Read all items

About, copyright, privacy and accessibility | Mail