Skip navigation.

Harold's Home

   Home
  
XML version of this site

PHP Scripts

Index
CLI fun
Mail on 404
HB-NS (NewsScript)

Downloads

Applescripts
APOD to Desktop
Dreamweaver Extensions

Stuff

Writings
Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Bananas
Strippers
Sample Movies

News

All articles in Cocoa

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

Mad Cocoa Skillz (absence of)
I'd like to tell you all how with the release of Webkit (to download the free webkit from Apple's developersite, you'll need a free account) I'm now a programming God and able to impress all the chicks with my Mad Cocoa Skillz.

Alas, this is not the case. The language is completely alien to me. I've stared and stared but even something seemingly simple as changing the UserAgent to anything other than the stupid Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/85 (KHTML, like Gecko) was a failure.

I think I'll stick to webdevelopment for now, and read logfiles filled with UserAgent strings, lovingly crafted by cunning artificers.

In any case: I don't know any chicks, so it's not all bad.

Show all items | Read all items | Show topics

About, copyright, privacy and accessibility | Mail