Reputation: 1699
Here i use GET
method to fetch data from one Url and display the data in table view.I used synchronous
to get data to display.
Needed:
I need to change this code to Asynchronous to display same data in table view.Here i just use my console to display data.
Here my code:
#import "ViewController.h"
#import <CommonCrypto/CommonDigest.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getdata];
}
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
-(void)getdata {
NSString *userName = @"[email protected]";
NSString *password = @"passer";
//encoding
NSData *Data = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [Data base64EncodedStringWithOptions:0];
base64String=[self sha256HashFor: base64String];
NSString *urlString = @"https://api.exampleurl/user23/doc";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
}
In this code i used base64 and shaa256 encoding.
Can any one help me to do with Asynchronous. I don't know how to do that. please help me with code explain. Thanks in advance!
Upvotes: 0
Views: 98
Reputation: 283
For making Asynchronous http request you can use :
[NSURLConnection sendAsynchronousRequest:httpRequest queue:[AppDelegate connectionQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
....
}];
or you can implement NSURLConnectionDataDelegate
protocol
First create a connection queue in app delegate file
+ (NSOperationQueue *)connectionQueue
{
dispatch_once(&once, ^{
connectionQueue = [[NSOperationQueue alloc] init];
[connectionQueue setMaxConcurrentOperationCount:2];
[connectionQueue setName:@"com.XYZ.connectionqueue"];
});
return connectionQueue;
}
-(void)getdata {
NSString *userName = @"[email protected]";
NSString *password = @"passer";
//encoding
NSData *Data = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [Data base64EncodedStringWithOptions:0];
base64String=[self sha256HashFor: base64String];
NSString *urlString = @"https://api.exampleurl/user23/doc";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
__block NSString *str;
[NSURLConnection sendAsynchronousRequest:httpRequest queue:[AppDelegate connectionQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];}];
NSLog(@"%@", str);
}
Upvotes: 3