How to config NSURLSessionConfiguration into the NSObject class?

℡╲_俬逩灬. 提交于 2019-12-06 13:07:56

Try this :

In interface file

@interface APISession : NSURLSession <NSURLSessionDownloadDelegate,NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionTaskDelegate>

+(void)apiCallSharedSessionGet:(NSString *)strURL withCompletionHandlar:(void (^) (NSString *response, NSError *error, int status)) completionBlock;

In Implementation file

@implementation APISession



+(void)apiCallSharedSessionGet:(NSString *)strURL withCompletionHandlar:(void (^) (NSString *response, NSError *error, int status)) completionBlock
{


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

   __block NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error!=nil)
        {
            completionBlock(nil,error,0);
            [task suspend];
        }
        else
        {
            NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
            NSLog(@"requestReply: %@", requestReply);
            completionBlock(requestReply,error,1);
            [task suspend];
        }

    }];
 [task resume];

}

If you want to get response from NSURLSessionDataTask before start receiving data, don't initial NSURLSessionDataTask with dataTaskWithURL: completionHandler. You should initial the data task with dataTaskWithURL and handle the delegate instead.

NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *urlSession=[NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
NSURL *servrurl=[NSURL URLWithString:GetOTP]; 
NSURLSessionDataTask *dataTask=[urlSession dataTaskWithURL:servrurl];

You should add conform to NSURLSessionDataDelegate in your delegate file.

In your delegate implementation:

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    NSLog(@"Response: %@", response);
    completionHandler(NSURLSessionResponseAllow); // Allow to continue this request
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    NSLog(@"Receive data");
}

Note that you should implement the second delegate method correctly to receive full data of your request, because data will not be received at once.

For more information, refer to this document: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveData:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!