macos - NSURLSessionDataTask 在 macOS 上不起作用, "A server with the specified hostname could not be found."

标签 macos nsurlsessiondatatask

为什么 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 目标,您必须在“功能”选项卡上显式启用“出站连接(客户端)”:

capabilities

在您执行此操作之前,所有出站连接都将失败,并显示 NSURLErrorCannotFindHost错误。

关于macos - NSURLSessionDataTask 在 macOS 上不起作用, "A server with the specified hostname could not be found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58042612/

相关文章:

python - 如何在 Mac 中使用 Python 获取事件窗口标题?

iphone - 如何使用 NSCalendar 或 NSDate 将 30 天生成为数组?

mysql - Ruby on Rails 3 无法通过 OSX 上的套接字 '/tmp/mysql.sock' 连接到本地 MySQL 服务器

ios - 请求与 postman 正常工作但在使用 iOS 请求时因协议(protocol)错误而失败

swift - 从 dataTask completionHandler 抛出错误

nsurlsession - 下载前获取 AVAssetDownloadTask 的大小

c++ - 在 dmg C++ 中运行 pkg

macos - CoreAnalytics::Client/FrameworkConfiguration::init 内神秘崩溃

ios - 当我使用 NSURLSessionDataTask 下载任务时进度条无法正常工作?

unit-testing - OCMock -[NSURLSessionDataTask 响应] 在 iOS 9 中无法识别的选择器崩溃