NSURLConnection delegate methods not running

I am running the following sample code from Apple -

        NSString *requestURL = [[NSString alloc] initWithString:@"http://google.com/"];         
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if(theConnection) {

            NSLog(@"The connection has started...");

        } else {

            NSLog(@"Connection has failed...");

        }

But the delegate methods below are not called. Can anyone help me out?

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
+3
source share
2 answers

If you are doing this in a background thread, the thread is likely to exit before calling delegates. Look at the call to your method, which starts the transfer right after the call to the method that starts the background thread, CFRunLoopRun (). Then in your callbacks connectionDidFinishLoading:and connection:didFailWithError:make sure that you also run:

CFRunLoopStop(CFRunLoopGetCurrent());

At the end of these methods, otherwise you will never return.

+3
source

? , , <NSURLConnectionDelegate>

:

@interface myClass : parentClass<NSURLConnectionDelegate>

!

+1

Source: https://habr.com/ru/post/1758321/


All Articles