Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AuthenticatedUserOperation.m
- // Tweeb
- //
- // Created by john on 12/28/09.
- // Copyright 2009 Mobomo LLC. All rights reserved.
- //
- #import "AuthenticatedUserOperation.h"
- #import "User.h"
- @implementation AuthenticatedUserOperation
- - (id)initWithUser:(User *)user {
- if ((self = [super initWithUser:user])) {
- responseData = [[NSMutableData alloc] initWithCapacity:2000];
- }
- return self;
- }
- - (void)dealloc {
- NSLog(@"deallocating auth operation");
- [responseData release];
- [_connection cancel];
- [_connection release];
- [super dealloc];
- }
- - (BOOL)isConcurrent {
- return YES;
- }
- - (BOOL)isExecuting {
- return _executing;
- }
- - (BOOL)isFinished {
- return _finished;
- }
- - (void)start {
- NSURLRequest *request = [NSURLRequest requestWithURL:[self userOperationURL]
- cachePolicy:NSURLCacheStorageNotAllowed
- timeoutInterval:USER_OPERATION_TIMEOUT];
- [self willChangeValueForKey:@"executing"];
- _executing = YES;
- [self didChangeValueForKey:@"executing"];
- _connection = [[NSURLConnection alloc] initWithRequest:request
- delegate:self
- startImmediately:YES];
- NSLog(@"Connection: %@", _connection);
- }
- - (void)doStop {
- [self willChangeValueForKey:@"executing"];
- [self willChangeValueForKey:@"finished"];
- _finished = YES;
- _executing = NO;
- [self didChangeValueForKey:@"executing"];
- [self willChangeValueForKey:@"finished"];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- if ([self isCancelled]) {
- [connection cancel];
- [self doStop];
- return;
- }
- [responseData appendData:data];
- }
- - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
- return NO;
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- NSLog(@"auth op finished loading");
- [APP_DELEGATE popNetworkActivity];
- if ([self isCancelled]) {
- [self doStop];
- return;
- }
- [self handleResponseDataInternal:responseData];
- [self save];
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- NSLog(@"Connection error: %@, %@", error, [error userInfo]);
- [self doStop];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- NSLog(@"got response: %@", response);
- }
- - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
- if ([self isCancelled]) {
- [connection cancel];
- [self doStop];
- return;
- }
- if ([challenge previousFailureCount] == 0) {
- NSURLCredential *newCredential;
- newCredential=[NSURLCredential credentialWithUser:[self.user screenName]
- password:[self.user password]
- persistence:NSURLCredentialPersistenceNone];
- [[challenge sender] useCredential:newCredential
- forAuthenticationChallenge:challenge];
- } else {
- [[challenge sender] cancelAuthenticationChallenge:challenge];
- }
- }
- @end
Add Comment
Please, Sign In to add comment