NSURLSessionDataTask 不适用于 macOS,"A server with the specified hostname could not be found."

NSURLSessionDataTask does not work on macOS, "A server with the specified hostname could not be found."

为什么 dataTaskWithURL 可以在 iOS 上运行,但不能在 macOS 上运行?

错误信息是:

Client-Error: A server with the specified hostname could not be found.

我的套路是这样的:

- (void)loadHTML {
    NSString *urlString = @"https://morph.zone/modules/newbb_plus/viewtopic.php?topic_id=12630&forum=10";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error != nil) {
            NSLog(@"Client-Error:%@",error.localizedDescription);
        }
        else {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) {
                NSLog(@"Server-Error:%ld",httpResponse.statusCode);
            }
            else {
                NSLog(@"Data downloaded");
            }
        }
    }];
    [downloadTask resume];
}

对于 macOS 目标,您必须在“功能”选项卡上明确启用“出站连接(客户端)”:

在您这样做之前,所有出站连接都将失败并出现 NSURLErrorCannotFindHost 错误。