Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSString *username = @"admin";
- NSString *password = @"test";
- NSString *authString = [NSString stringWithFormat:@"%@:%@",
- username,
- password];
- // 2 - convert authString to an NSData instance
- NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
- // 3 - build the header string with base64 encoded data
- NSString *authHeader = [NSString stringWithFormat: @"Basic %@",
- [authData base64EncodedStringWithOptions:0]];
- // 4 - create an NSURLSessionConfiguration instance
- NSURLSessionConfiguration *sessionConfig =
- [NSURLSessionConfiguration defaultSessionConfiguration];
- // 5 - add custom headers, including the Authorization header
- [sessionConfig setHTTPAdditionalHeaders:@{
- @"Accept": @"application/json",
- @"Authorization": authHeader
- }
- ];
- // 6 - create an NSURLSession instance
- NSURLSession *session =
- [NSURLSession sessionWithConfiguration:sessionConfig delegate:self
- delegateQueue:nil];
- // 7 - create an NSURLSessionDataTask instance
- NSString *urlString = @"http://test.myserver.am/api/authentication/Login?username=admin&password=test";
- NSURL *url = [NSURL URLWithString:urlString];
- NSURLSessionDataTask *task = [session dataTaskWithURL:url
- completionHandler:
- ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
- if (error)
- {
- // do something with the error
- return;
- }
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
- if (httpResponse.statusCode == 200)
- {
- arrTokenData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
- [self getDomain];
- } else {
- // failure: do something else on failure
- NSLog(@"httpResponse code: %@", [NSString stringWithFormat:@"%ld", (unsigned long)httpResponse.statusCode]);
- NSLog(@"httpResponse head: %@", httpResponse.allHeaderFields);
- return;
- }
- }];
- // 8 - resume the task
- [task resume];
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- NSString *authValue = [arrTokenData valueForKey:@"Token"];
- //Configure session with common header fields
- NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
- sessionConfiguration.HTTPAdditionalHeaders = @{@"bearer": authValue};
- NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
- NSString *url = @"http://test.myserver.am/api/mobile/LookUps/getuserdata";
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
- NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- if (!error) {
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
- if (httpResponse.statusCode == 200)
- {
- NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
- //Process the data
- }
- }
- }];
- [task resume];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement