NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setHTTPBody:jsonData];
// configure NSURLSessionConfiguration with request timeout
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
// set request timeout
defaultConfigObject.timeoutIntervalForRequest = 120.0;
// create NSURLSession object
// Working fine with below instance of default session but its taking a lot of time to fetch response.
//NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
// set NSURLSessionDataTask
@try {
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
}];
[dataTask resume];
}
The task never completes because it never gets started. You have to manually start the data task using its resume() method.
And Don't use the dataTask object inside the try block.
if ([self checkNetworkStatus]) {
@try {
// Create the data and url
NSString *encryptedString = [self createRequest:userContext objectType:deviceObjectType projectType:projectId];
NSDictionary *dictRequest = @{REQ_KEY_REQUEST: encryptedString};
requestString = [JSONHelper dictionaryToJson:dictRequest];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@Data",globals.API_Base_URL]];
// Create Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
request.HTTPMethod = @"POST"; // For Post
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
int strLength = (int)requestString.length;
[request setValue:[NSString stringWithFormat:@"%d", strLength] forHTTPHeaderField:@"Content-Length"];
NSData *dataRequest = [requestString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = dataRequest;`enter code here`
id delegateValue = self;
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:delegateValue
delegateQueue:[NSOperationQueue mainQueue]];
//NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *responseData, NSURLResponse *response, NSError *error) {
// ...
[self destroyNetworkCache];
// [[NSURLCache sharedURLCache] storeCachedResponse:nil forRequest:urlReq];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
if (! error)
{
[self parseResponse:responseData forObjectType:deviceObjectType andTag:tag withDelegate:del];
}
else
{
NSString *errMsg = error.description;
if (errMsg.length <= 0) {
errMsg = NSLocalizedString(@"msg_network_error", @"msg_network_error");
}
else if (errMsg.length > 0 && [errMsg rangeOfString:@"timed out"].length != 0)
{
errMsg = NSLocalizedString(@"msg_request_timed_out", @"msg_request_timed_out");
}
else if ([self checkForURLDomainError:errMsg])
{
errMsg = NSLocalizedString(@"msg_network_error", @"msg_network_error");
}
if (tag < 0)
{
if ([del conformsToProtocol:@protocol(WTConnectionServiceDelegate)])
{
if ([del respondsToSelector:@selector(wtConnectionService:forObjectType:didFailedWithError:)])
{
[del wtConnectionService:nil forObjectType:deviceObjectType didFailedWithError:errMsg];
return;
}
}
}
else
{
if ([del conformsToProtocol:@protocol(WTConnectionServiceDelegate)])
{
if ([del respondsToSelector:@selector(wtConnectionService:forObjectType:andTag:didFailedWithError:)])
{
[del wtConnectionService:nil forObjectType:deviceObjectType andTag:tag didFailedWithError:errMsg];
return;
}
}
}
}
});
}];
[task resume];
}
@catch (NSException *exception) {
[self handleException:exception];
}
@finally {
}
}
Below Is delegate methods
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
-(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.