Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com/"]]
  2. queue:[NSOperationQueue mainQueue]
  3. completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  4.  
  5.  
  6. //Do Stuff
  7.  
  8. }];
  9.  
  10. - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  11. {
  12.  
  13. if ([challenge previousFailureCount] > 0) {
  14. NSLog(@"Authentication Failure");
  15. [connection cancel];
  16. }
  17. else
  18. {
  19.  
  20. NSURLCredential *credential = [NSURLCredential credentialWithUser:self.username
  21. password:self.password
  22. persistence:NSURLCredentialPersistenceForSession];
  23. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  24. }
  25.  
  26. }
  27.  
  28. NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/"]];
  29.  
  30. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", userName, password];
  31. NSString *authValue = [NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)];
  32. [urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
  33.  
  34. static NSString * AFBase64EncodedStringFromString(NSString *string) {
  35. NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
  36. NSUInteger length = [data length];
  37. NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
  38.  
  39. uint8_t *input = (uint8_t *)[data bytes];
  40. uint8_t *output = (uint8_t *)[mutableData mutableBytes];
  41.  
  42. for (NSUInteger i = 0; i < length; i += 3) {
  43. NSUInteger value = 0;
  44. for (NSUInteger j = i; j < (i + 3); j++) {
  45. value <<= 8;
  46. if (j < length) {
  47. value |= (0xFF & input[j]);
  48. }
  49. }
  50.  
  51. static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  52.  
  53. NSUInteger idx = (i / 3) * 4;
  54. output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
  55. output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
  56. output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
  57. output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
  58. }
  59.  
  60. return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
  61. }
  62.  
  63. [NSURLConnection sendAsynchronousRequest:urlRequest
  64. queue:[NSOperationQueue mainQueue]
  65. completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  66.  
  67. }];
  68.  
  69. //HTTP Authentication
  70. NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"Username", @"Password"]];
  71. NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
  72. NSString *authValue = [authData base64Encoding];
  73.  
  74. //Set up Request:
  75. NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
  76. [request setURL:[NSURL URLWithString:url]];
  77. //...
  78. // Pack in the user credentials
  79. [request setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];
  80.  
  81. // Send the asynchronous request as usual
  82. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *error) {
  83.  
  84. // Do stuff..
  85.  
  86. }
  87.  
  88. NSURLConnection is deprecated in iOS 9, try this code which is from LazyTableImages example from here [https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html][1]
  89.  
  90. @property (nonatomic, strong) NSURLSessionDataTask *sessionTask;
  91.  
  92.  
  93.  
  94. //somwere in your code
  95.  
  96. NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
  97. initWithURL:[NSURL URLWithString:self.postLink] ];
  98.  
  99. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  100. [request setHTTPShouldHandleCookies:NO];
  101. [request setTimeoutInterval:60];
  102. [request setHTTPMethod:@"POST"];
  103. [request addValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]
  104. forHTTPHeaderField:@"ACCESSTOKEN"];
  105.  
  106.  
  107. self.sessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
  108. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  109. //do completion staff here
  110. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement