Skip to content

Instantly share code, notes, and snippets.

@davehimself
Created May 15, 2010 19:41
Show Gist options
  • Save davehimself/402368 to your computer and use it in GitHub Desktop.
Save davehimself/402368 to your computer and use it in GitHub Desktop.
#import "CampaignList.h"
#import "VertsterClient.h"
@implementation CampaignList
@synthesize campaigns;
- (id)init {
if (self = [super init]) {
VertsterClient *client =
[[VertsterClient alloc] initWithUsername:@"xxx"
andApiKey:@"xxx"];
[client setDelegate: self];
[client connectWithUrlString:@"http://api.vertster.com/campaigns.xml"];
}
return self;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Delegation");
}
- (void)dealloc {
[campaigns release];
[super dealloc];
}
@end
#import "VertsterClient.h"
@implementation VertsterClient
@synthesize apiKey;
@synthesize apiUrlString;
@synthesize responseBody;
@synthesize username;
# pragma mark VertsterClient Instance Methods
- (id)initWithUsername:(NSString *)user andApiKey:(NSString *)key {
if (self = [super init]) {
self.username = user;
self.apiKey = key;
}
return self;
}
- (void)connectWithUrlString:(NSString *)theUrlString {
NSURL *url = [[NSURL URLWithString:theUrlString] retain];
urlRequest = [NSMutableURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
# pragma mark NSURLConnection Methods
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
{
return request;
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Response recieved");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog(@"Failure: %s", [error localizedDescription]);
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Auth request sent");
NSURLCredential *credentials =
[[NSURLCredential alloc]
initWithUser:username
password:apiKey
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge];
[credentials release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Received data");
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Received %d total bytes", [responseData length]);
responseBody =
[[NSString alloc]
initWithBytes:[responseData bytes]
length:[responseData length]
encoding:NSUTF8StringEncoding];
if ([delegate respondsToSelector:@selector(connectionDidFinsihLoading:)]) {
[delegate connectionDidFinishLoading:connection];
}
}
#pragma mark Memory Management
- (void)dealloc {
[apiKey release];
[apiUrlString release];
[urlRequest release];
[responseBody release];
[responseData release];
[username release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment