gpt4 book ai didi

ios - 如何将 NSURLSessionConfiguration 配置到 NSObject 类中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:31:44 25 4
gpt4 key购买 nike

我想从 NSObject 获取服务器响应,当我收到响应时,它已返回到 viewController。因为我在服务器调用 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)文件中

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

}

关于ios - 如何将 NSURLSessionConfiguration 配置到 NSObject 类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com