i want to get server response from NSObject
when i m getting a response,it has return to viewController.as i implement on server call into NSObject
class then i called to the NSObject
method but before server response my NSString
have been return as null
.
NSObject class :
@interface getServerCallClassMethod :
NSObject<NSURLSessionDelegate>
{
NSString *ResponceStr;
}
-(NSString *)getServerCall:(NSString *)mobleNo serverUrl:(NSString *)url;
@end
implement part :
@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");
If you want to get response from
NSURLSessionDataTask
before start receiving data, don't initialNSURLSessionDataTask
withdataTaskWithURL: completionHandler
. You should initial the data task withdataTaskWithURL
and handle the delegate instead.You should add conform to
NSURLSessionDataDelegate
in your delegate file.In your delegate implementation:
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:
Try this :
In interface file
In Implementation file