[英]Pass a delegate as a parameter for NSURLSession
I have a class named RestService which I use all over my app to perform several synchronous requests to a web service. 我有一个名为RestService的类,该类在我的整个应用程序中用于对Web服务执行几个同步请求。 I added a new method to this class to perform an asynchronous request which again I want to reuse all over my app.
我向此类添加了一个新方法来执行异步请求,我再次希望在整个应用程序中重用。 This is the code for that new method:
这是该新方法的代码:
- (void)backgroundExecutionOfService:(NSString *)serviceName
withParameters:(NSDictionary *)parameters
inView:(UIView *)view
withDelegate:(UIViewController *)delegate
{
NSString *serviceUrl = @"http://MyWebServer/public/api/clients/5";
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.allowsCellularAccess = YES;
sessionConfig.timeoutIntervalForRequest = 10;
sessionConfig.timeoutIntervalForResource = 10;
sessionConfig.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *session;
session = [NSURLSession sessionWithConfiguration:sessionConfig
delegate:delegate
delegateQueue:nil];
NSURLSessionDownloadTask *getFileTask;
getFileTask = [session downloadTaskWithURL:[NSURL URLWithString:serviceUrl]];
[getFileTask resume];
}
But XCode is giving me a warning about using that parameter as a delegate (Sending UIViewController * __strong' to parameter of incompatible type 'id< NSURLSessionDelegate > _Nullable'). 但是XCode给我有关使用该参数作为委托的警告(将UIViewController * __strong发送给不兼容类型'id <NSURLSessionDelegate> _Nullable'的参数)。 I made sure that the view controller I'm sending as a parameter has declared < NSURLSessionDelegate > in .h and I created the delegate methods in the ViewControllers's implementation file.
我确保要作为参数发送的视图控制器已在.h中声明了<NSURLSessionDelegate>,并且在ViewControllers的实现文件中创建了委托方法。
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error{
NSLog(@"Became invalid with error: %@", [error localizedDescription]);
}
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{
NSLog(@"Received challenge");
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
NSLog(@"Did finish events for session: %@", session);
}
The app doesn't crash but the delegate methods are never called. 该应用程序不会崩溃,但永远不会调用委托方法。 The synchronous web services work as expected.
同步Web服务按预期方式工作。
That happened because a UIViewController class don't conform a NSURLSessionDelegate protocol. 发生这种情况是因为UIViewController类不符合NSURLSessionDelegate协议。 To resolve that discrepancy just change the method's signature to this like:
要解决这种差异,只需将方法的签名更改为:
- (void)backgroundExecutionOfService:(NSString *)serviceName withParameters:(NSDictionary *)parameters inView:(UIView *)view withDelegate:(id<NSURLSessionDelegate>)delegate{
//... your code
}
And "read up on the basics of delegates." 和“阅读代表的基础知识”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.