Guest User

Untitled

a guest
Jul 21st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. //
  2. // AuthenticatedUserOperation.m
  3. // Tweeb
  4. //
  5. // Created by john on 12/28/09.
  6. // Copyright 2009 Mobomo LLC. All rights reserved.
  7. //
  8.  
  9. #import "AuthenticatedUserOperation.h"
  10.  
  11. #import "User.h"
  12.  
  13.  
  14. @implementation AuthenticatedUserOperation
  15.  
  16.  
  17. - (id)initWithUser:(User *)user {
  18. if ((self = [super initWithUser:user])) {
  19. responseData = [[NSMutableData alloc] initWithCapacity:2000];
  20. }
  21. return self;
  22. }
  23.  
  24.  
  25. - (void)dealloc {
  26. NSLog(@"deallocating auth operation");
  27. [responseData release];
  28. [_connection cancel];
  29. [_connection release];
  30. [super dealloc];
  31. }
  32.  
  33.  
  34. - (BOOL)isConcurrent {
  35. return YES;
  36. }
  37.  
  38.  
  39. - (BOOL)isExecuting {
  40. return _executing;
  41. }
  42.  
  43.  
  44. - (BOOL)isFinished {
  45. return _finished;
  46. }
  47.  
  48.  
  49. - (void)start {
  50. NSURLRequest *request = [NSURLRequest requestWithURL:[self userOperationURL]
  51. cachePolicy:NSURLCacheStorageNotAllowed
  52. timeoutInterval:USER_OPERATION_TIMEOUT];
  53.  
  54. [self willChangeValueForKey:@"executing"];
  55. _executing = YES;
  56. [self didChangeValueForKey:@"executing"];
  57. _connection = [[NSURLConnection alloc] initWithRequest:request
  58. delegate:self
  59. startImmediately:YES];
  60. NSLog(@"Connection: %@", _connection);
  61. }
  62.  
  63.  
  64. - (void)doStop {
  65. [self willChangeValueForKey:@"executing"];
  66. [self willChangeValueForKey:@"finished"];
  67. _finished = YES;
  68. _executing = NO;
  69. [self didChangeValueForKey:@"executing"];
  70. [self willChangeValueForKey:@"finished"];
  71. }
  72.  
  73.  
  74. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  75. if ([self isCancelled]) {
  76. [connection cancel];
  77. [self doStop];
  78. return;
  79. }
  80.  
  81. [responseData appendData:data];
  82. }
  83.  
  84.  
  85. - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
  86. return NO;
  87. }
  88.  
  89.  
  90. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  91. NSLog(@"auth op finished loading");
  92.  
  93. [APP_DELEGATE popNetworkActivity];
  94.  
  95. if ([self isCancelled]) {
  96. [self doStop];
  97. return;
  98. }
  99.  
  100. [self handleResponseDataInternal:responseData];
  101. [self save];
  102. }
  103.  
  104.  
  105. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  106. NSLog(@"Connection error: %@, %@", error, [error userInfo]);
  107. [self doStop];
  108. }
  109.  
  110.  
  111. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  112. NSLog(@"got response: %@", response);
  113. }
  114.  
  115.  
  116. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  117. if ([self isCancelled]) {
  118. [connection cancel];
  119. [self doStop];
  120. return;
  121. }
  122.  
  123. if ([challenge previousFailureCount] == 0) {
  124. NSURLCredential *newCredential;
  125. newCredential=[NSURLCredential credentialWithUser:[self.user screenName]
  126. password:[self.user password]
  127. persistence:NSURLCredentialPersistenceNone];
  128. [[challenge sender] useCredential:newCredential
  129. forAuthenticationChallenge:challenge];
  130. } else {
  131. [[challenge sender] cancelAuthenticationChallenge:challenge];
  132. }
  133. }
  134.  
  135.  
  136. @end
Add Comment
Please, Sign In to add comment