- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想从 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/
我已经实现了以 20 个为一组下载大小为 1-2 MB 的 block 的代码。 session 完成前 20 个下载请求后,它将在 session 中设置新的 20 个请求。我已经在控制台日志中的真
找不到关于此实例变量的任何文档。 即使应用已移至后台,它是否与能够处理对网络请求的响应有关? 最佳答案 来自 SDK 的 NSURLSession 类注释: “为创建的任何 tcp 套接字启用扩展后台
我正在构建一个应用程序,用户可以在其中从应用程序和共享扩展启动后台文件上传。用户应该能够监控从主应用程序上传的任何进度。 在从扩展程序启动上传的情况下,我需要创建一个后台 session 配置,其 i
是否可以忽略设备网络设置中的代理设置,而不是在使用 NSURLSession 时不使用代理? 基本上,如果连接失败,我想退回到不使用代理。 谢谢! 最佳答案 我非常确定在 session 配置中提供一
我正在从网上下载一个文件。文件有时很大,有时可能会达到 100MB,我想在应用程序进入后台或设备锁定时继续下载。为此,我正在使用 AFNetworking 3.0 [NSURLSessionConfi
授权 header 在 NSURLSessionConfiguration 中设置,但是它没有附加到 NSURLSessionDataTask。这是 Foundation framework 中的错误
我正在尝试运行后台 NSURLSessionDownloadTask。但是,我注意到在任务实际开始之前大约需要 30 秒。当我使用 defaultSessionConfiguration 时,任务几乎
当 session 设置为在后台工作时(通过 backgroundSessionConfiguration 调用),NSURLSessionConfiguration 的新超时属性似乎不起作用。 有人
我正在尝试用 NSUrlSession 替换一些古老的网络代码,但是将 HTTPMaximumConnectionsPerHost 设置为 1 没有任何效果。这个请求代码被调用了 170 次,但它在任
我在官方文档中看到,NSURLSessionConfiguration 和 NSURLRequest 共享一些相同或相似的属性。比如NSURLRequestCachePolicy、networkSer
即使在设置缓存策略之后,我的 NSURLSession 仍在加载缓存数据: let url = NSURL(string: urlString); var sessionConfig = NSURLS
我正在使用代码: let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.r
我遵循了本教程 Downloading files in background with URLSessionDownloadTask 还有这个苹果文档 Downloading Files in th
从一开始,我是如何配置NSURLSession的: auto securityPolicy = self.securityPolicy; NSURLSessionConfiguration *conf
我想从 NSObject 获取服务器响应,当我收到响应时,它已返回到 viewController。因为我在服务器调用 NSObject 类时实现,然后我调用NSObject 方法,但在服务器响应之前
有了新的 NSURLSession,NSURLSessionConfiguration 对象上现在有一个 timeoutIntervalForRequest 用于创建 session 。 但是 NSU
我正在构建一个 iOS 应用程序,它在后台将一堆文件上传到 Amazon S3 - 我正在使用 NSURLSession 结合 NSURLSessionConfiguration 为此和这似乎运作良好
我正在使用 NSURLSession 进行 REST 网络服务调用。我已经在 NSURLSessionConfiguration.HTTPAdditionalHeaders 中设置了网络服务的内容类型
此方法设置背景对象。 - (void) downloadWithURL: (NSMutableArray *)urlArray pathArr: (NSMutableArra
我用一行代码启动了一个新的 Swift 应用程序: 让 config = NSURLSessionConfiguration.defaultSessionConfiguration() 和链接的二进制
我是一名优秀的程序员,十分优秀!