Using Shrty
This simple example assumes a few things.1) _shrty is a member variable.
2) shrtySvcWithName - We're creating a service instance that doesn't require a name or password. (E.G. IsGd)
3) self implements the ShrtyDelegateProtocol protocol.
First things first.
#import "Shrty.h"Shrty.h will include everything you need to use Shrty.
Next you'll need to create a ShrtySvc instance. The code below illustrates how you may choose to do that. In this case we're passing the service name, Is.Gd, into a helper method to create our instance. There are constants defined in ShrtySvcConst.h for all the services supported by Shrty.
In the example above I've removed some code for brevity's sake. Another thing to note is use of the helper method _createShrtyInstanceFromServiceName, included below. This method is part of ShrtyAppController.m, you'll find it in the Shrty source, it's part of the test project.
// Go create an instance of the IsGd service.
[self _createShrtyInstanceFromServiceName:kShrtyIsGdSvcName];
if (nil != _shrty)
{
NSString* urlToShorten = @"http://rob.crabapples.net/";
// Shorten the URL.
[_shrty shortenUrl:urlToShorten];
}
If all goes as expected you should expect to receive a callback to your didReceiveShortenedURL method.
- (void)_createShrtyInstanceFromServiceName:(NSString*)serviceName
{
[_shrty release];
_shrty = [ShrtySvc shrtySvcWithName:serviceName userName:nil userPassword:nil delegate:self];
}
Then you can do with that URL what you please, post it to Twitter, or Facebook, or whatever.
- (void) didReceiveShortenedURL:(NSString*)shortenedURL
resultCode:(NSString*)resultCode
{
if (nil == shortenedURL)
{
NSLog(@"GOT A NIL RESULT, DOH!");
}
else
{
NSLog(shortenedURL);
}
}
Labels: Cocoa, Development, Objective-C, Open Source
About this entry
You’re currently reading “Using Shrty,” an entry on Rob Fahrni
- Published:
- 12:45 PM
- by Rob
0 Comments (Post a Comment)