NSURLSession question

Apparently NSURLConnection is deprecated in 10.11, so I was trying to figure out NSURLSession. For some reason code in the completionHandler is never run when building for OS X, but if I plug the same code in an iPhone project it id executed as expected.

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *urlAddress = @"http://www.google.com/images/logos/ps_logo2.png";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSLog(@"Download image");
        NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"In completionHandler");
        } ];
        
        [task resume];
    }
    return 0;
}

Anyone have any idea why this is happening?

NSURLSession needs a run loop.

You can either create one by hand if you want to run as a command line tool, or run that piece of code in a Cocoa application.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
        NSString *urlAddress = @"http://www.google.com/images/logos/ps_logo2.png";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSLog(@"Download image");
        NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog (@"In completionHandler: %@ error: %@", response, error);
        } ];
        
        [task resume];
        [runLoop run];
    }
    return 0;
}

[quote=“ibex10”]NSURLSession needs a run loop.

You can either create one by hand if you want to run as a command line tool, or run that piece of code in a Cocoa application.
[/quote]

Thanks. The problem is now it never leaves the session. I tried stopping it using a while loop with a bool that changes in completion handler, and explicitly calling CFRunLoopStop(CFRunLoopGetCurrent()) in the completion handler and the loop, but it never exits.

[code]
int main(int argc, const char * argv[]) {
@autoreleasepool {

    NSString *urlAddress = @"http://www.google.com/images/logos/ps_logo2.png";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSLog(@"Download image");
    NSRunLoop * runLoop = [NSRunLoop currentRunLoop]; // Needed for NSURLSession
    __block NSData *data;
    __block BOOL shouldKeepRunning = YES;
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *sessionData, NSURLResponse *response, NSError *error) {
        NSLog(@"In completionHandler");
        NSLog(@"Got response %@ with error %@.\n", response, error);
        NSLog(@"DATA:\n%@\nEND DATA\n",
              [[NSString alloc] initWithData: sessionData
                                    encoding: NSUTF8StringEncoding]);
        data = sessionData;
        shouldKeepRunning = NO;
        CFRunLoopStop(CFRunLoopGetCurrent());
    } ];
    
    [task resume];
    
    while (shouldKeepRunning) {
        NSLog(@"Loop");
        [runLoop runUntilDate:[NSDate distantFuture]];
        shouldKeepRunning = NO;
    };
    
    NSLog(@"data bytes: %lu", [data length]);

}
return 0;

}[/code]

Maybe I am looking at this wrong. This seems overly complex for something that used to be done with:

I gave you that example on purpose, hoping that it would encourage you to start digging deeper into the architecture of a Cocoa application.

Apple provides developers with freely accessible documentation. Get into the habit of reading them seriously in addition to reading BNR books and googling things.