Domanda

I'm trying to develop a simple, one file proof of concept. The code is as follows:

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;
}

The request to example.com over HTTPS uses a private CA, so I need to trust that CA. To trust that CA, I need to add some code to NSURLConnectionDelegate's -connection:didReceiveAuthenticationChallenge:

The problem is I need the NSURLConnection's delegate for the callback, and the only way I know how to do it is with another separate object.

Is it possible to flatten the delegate into a static function so I can keep all code in a single file?

È stato utile?

Soluzione 2

It's not possible to do what you're literally asking; the NSURLConnction must have an object as its delegate, because it's going to send specific messages, from the delegate protocol, to that object. Functions can't respond to messages. (There's no reason you can't define your delegate class in this file, however.)

There are some options for creating on-the-spot delegates which you might find useful, and in this particular case, you could use +sendSynchronousRequest:returningResponse:error:

Altri suggerimenti

you don't have to have one file for one class. you can add classes in your main.m

But you will find another problem: your code will execute so fast that NSURLConnection won't have enough time to wait for anything to return. You could add a NSRunLoop 1 2.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top