我正在尝试开发一个简单的文件证明概念。代码如下:

int main(int argc, const char *argv[])
{
    NSString* requestString = @"https://www.example.com";
    NSURL* requestUrl = [NSURL URLWithString:requestString];

    NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl
                                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                         timeoutInterval:10.0f];

    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return 0;
}


通过HTTPS向example.com发出的请求使用私有CA,因此我需要信任该CA。要信任该CA,我需要向NSURLConnectionDelegate-connection:didReceiveAuthenticationChallenge:添加一些代码

问题是我需要NSURLConnection的委托进行回调,而我知道如何做的唯一方法是使用另一个单独的对象。

是否可以将委托展平为静态函数,以便将所有代码保存在单个文件中?

最佳答案

不可能按照您的要求去做。 NSURLConnction必须有一个对象作为其委托,因为它将要从委托协议向该对象发送特定的消息。函数无法响应消息。 (但是,没有理由不能在此文件中定义您的委托类。)

creating on-the-spot delegates的某些选项可能会有用,在这种情况下,您可以使用+sendSynchronousRequest:returningResponse:error:

关于objective-c - 将委托(delegate)转换为静态函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23258334/

10-14 20:53
查看更多