Hello everyone.
I was wondering if it is recommended to use NSURLSessionDelegate (NSURLSessionDataDelegate, NSURLSessionTaskDelegate) in your class and how or to use simply the NSURLSessionDataTask.
Using NSURLSessionDelegate :
//In header file
interface ViewController : UIViewController <NSURLSessionDataDelegate, NSURLSessionTaskDelegate>
//In .m file implement the following:
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
or
NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
}];
[task resume];
The authorization is used, it is BASIC.
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.testwebpage.com"]];
//Get values from textfiedds
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.txtfield_username.text
password:self.txtfield_password.text
persistence:NSURLCredentialPersistenceNone];
NSString *authStr = [newCredential user];
authStr = [authStr stringByAppendingString:@":"];
authStr = [authStr stringByAppendingString:[newCredential password] ];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
Regards,
kalgik