当我得到响应时,我想从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");发布于 2016-05-16 08:41:45
试试这个:
接口文件中的
@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];
}发布于 2016-05-16 08:33:16
如果您想在开始接收数据之前从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");
}请注意,您应该正确地实现第二个委托方法,以接收请求的全部数据,因为数据不会立即收到。
有关更多信息,请参阅本文档:ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveData
https://stackoverflow.com/questions/37248688
复制相似问题