如何configurationNSURLSessionConfiguration到NSObject类?

我想从NSObject得到服务器响应,当我得到一个响应,它已经返回到viewController.as我实现服务器调用到NSObject类然后我调用NSObject方法,但在服务器响应我的NSString已经返回为null

  NSObject class : @interface getServerCallClassMethod : NSObject<NSURLSessionDelegate> { NSString *ResponceStr; } -(NSString *)getServerCall:(NSString *)mobleNo serverUrl:(NSString *)url; @end 

实施部分:

@implementation getServerCallClassMethod

 -(NSString *)getServerCall:(NSString *)mobleNo serverUrl:(NSString *)url{ NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *urlSession=[NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURL *servrurl=[NSURL URLWithString:GetOTP]; NSURLSessionDataTask *dataTask=[urlSession dataTaskWithURL:servrurl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable connectionError) { if (data.length > 0 && connectionError == nil){ ResponceStr=@"success"; } else{ ResponceStr=@"fail"; } }]; [dataTask resume]; }); return ResponceStr; } In Vc : getServerCallClassMethod *GetServerCallMethod=[[getServerCallClassMethod alloc]init]; NSString *valide=[GetServerCallMethod getServerCall:@"800000000" serverUrl:@"http://www.gg.com"]; NSLog(@"valide"); 

尝试这个 :

在接口文件中

 @interface APISession : NSURLSession <NSURLSessionDownloadDelegate,NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionTaskDelegate> +(void)apiCallSharedSessionGet:(NSString *)strURL withCompletionHandlar:(void (^) (NSString *response, NSError *error, int status)) completionBlock; 

在实现文件中

 @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]; } 

如果要在开始接收数据之前从NSURLSessionDataTask获取响应,请不要使用dataTaskWithURL: completionHandler初始化NSURLSessionDataTask 。 您应该使用dataTaskWithURL初始化数据任务,然后处理委托。

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

您应该在委托文件中添加符合NSURLSessionDataDelegate

在你的委托实现中:

 - (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"); } 

请注意,您应该正确地实施第二个委托方法来接收您的请求的完整数据,因为数据不会一次被接收。

有关更多信息,请参阅此文档: https : //developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession : dataTask : didReceiveData :