Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. NSString *username = @"admin";
  2. NSString *password = @"test";
  3. NSString *authString = [NSString stringWithFormat:@"%@:%@",
  4. username,
  5. password];
  6.  
  7. // 2 - convert authString to an NSData instance
  8. NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
  9.  
  10. // 3 - build the header string with base64 encoded data
  11. NSString *authHeader = [NSString stringWithFormat: @"Basic %@",
  12. [authData base64EncodedStringWithOptions:0]];
  13.  
  14. // 4 - create an NSURLSessionConfiguration instance
  15. NSURLSessionConfiguration *sessionConfig =
  16. [NSURLSessionConfiguration defaultSessionConfiguration];
  17.  
  18. // 5 - add custom headers, including the Authorization header
  19. [sessionConfig setHTTPAdditionalHeaders:@{
  20. @"Accept": @"application/json",
  21. @"Authorization": authHeader
  22. }
  23. ];
  24.  
  25. // 6 - create an NSURLSession instance
  26. NSURLSession *session =
  27. [NSURLSession sessionWithConfiguration:sessionConfig delegate:self
  28. delegateQueue:nil];
  29.  
  30. // 7 - create an NSURLSessionDataTask instance
  31. NSString *urlString = @"http://test.myserver.am/api/authentication/Login?username=admin&password=test";
  32. NSURL *url = [NSURL URLWithString:urlString];
  33. NSURLSessionDataTask *task = [session dataTaskWithURL:url
  34. completionHandler:
  35. ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
  36. if (error)
  37. {
  38. // do something with the error
  39.  
  40. return;
  41. }
  42.  
  43. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  44. if (httpResponse.statusCode == 200)
  45. {
  46. arrTokenData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
  47.  
  48. [self getDomain];
  49. } else {
  50. // failure: do something else on failure
  51. NSLog(@"httpResponse code: %@", [NSString stringWithFormat:@"%ld", (unsigned long)httpResponse.statusCode]);
  52. NSLog(@"httpResponse head: %@", httpResponse.allHeaderFields);
  53.  
  54. return;
  55. }
  56. }];
  57.  
  58. // 8 - resume the task
  59. [task resume];
  60.  
  61. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  62.  
  63. NSString *authValue = [arrTokenData valueForKey:@"Token"];
  64.  
  65. //Configure session with common header fields
  66. NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  67. sessionConfiguration.HTTPAdditionalHeaders = @{@"bearer": authValue};
  68.  
  69. NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
  70.  
  71. NSString *url = @"http://test.myserver.am/api/mobile/LookUps/getuserdata";
  72. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  73.  
  74. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  75. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  76. if (!error) {
  77. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  78. if (httpResponse.statusCode == 200)
  79. {
  80. NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
  81.  
  82. //Process the data
  83. }
  84. }
  85.  
  86. }];
  87. [task resume];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement