如何将NSURLSessionConfiguration配置为NSObject类? [英] How to config NSURLSessionConfiguration into the NSObject class?
本文介绍了如何将NSURLSessionConfiguration配置为NSObject类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在响应时从NSObject
获得服务器响应,它已经返回到viewController.as,因为我在服务器上实现了对NSObject
类的调用,然后我调用了NSObject
方法,但是在服务器响应之前NSString
已作为null
返回.
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
实施部分:
@implementation getServerCallClassMethod
@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];
}
这篇关于如何将NSURLSessionConfiguration配置为NSObject类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文