【Mac App】Domain=NSURLErrorDomain Code=-1003

ちょっと訳あってMacのアプリを作っていたのですが、お試しで・・・httpの通信をしようと思ったらエラーになったので、ちょっとハマってしまいました。iOSのアプリとは若干設定とかが違うのですね。

やったこと

http通信を試したくて以下のようなコードを書きました。iOSとコード全然変わらないですね。Macのアプリもすぐ作れそうだな〜なんて思っていました。

let url: URL = URL(string: "https://www.google.co.jp/")!
let task: URLSessionTask = URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
    print(response)
})
task.resume()

ところがこれを実行すると・・・

エラー発生

以下のようなエラーが発生して通信することが出来ませんでした。

2020-01-12 18:29:45.264928+0900 Xxxxxx[7909:337716] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:16 Err:-1 Errno:1 Operation not permitted
2020-01-12 18:29:45.265607+0900 Xxxxxx[7909:337716] [] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)
2020-01-12 18:29:45.266608+0900 Xxxxxx[7909:337716] Connection 1: received failure notification
2020-01-12 18:29:45.266685+0900 Xxxxxx[7909:337716] Connection 1: failed to connect 10:-72000, reason -1
2020-01-12 18:29:45.266724+0900 Xxxxxx[7909:337716] Connection 1: encountered error(10:-72000)
2020-01-12 18:29:45.268833+0900 Xxxxxx[7909:337717] Task <02BA88D7-55DF-4A21-9B44-E18872F3D8A4>.<1> HTTP load failed, 0/0 bytes (error code: -1003 [10:-72000])
2020-01-12 18:29:45.272301+0900 Xxxxxx[7909:337717] Task <02BA88D7-55DF-4A21-9B44-E18872F3D8A4>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x600000cac750 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://www.google.co.jp/, NSErrorFailingURLKey=https://www.google.co.jp/, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.}

なんだろうな〜って調べてみると・・・

解決

どうもMacのアプリだと、設定が必要そうです。

  1. Project Navigatorでアプリのルートをクリック
  2. Signing & Capabilitiesをクリック
  3. App SandboxのOutgoing Connections (client)をチェック

上記のチェックを入れました。
これで繋がるようになりました。
iOSの場合ってこんなのいらなかったですよね?

参考サイト

Error Domain=NSURLErrorDomain Code=-1003 “A server with the specified hostname could not be found

関連記事

【Spotify】spotify_token_swap.php

【Ubuntu】Ubuntuを19.04にアップグレードしてみた

【Swift】UIViewControllerでStatic CellsのTab...

【Ubuntu】UnuntuにSwiftをインストールしてみた件

【Objective-C】Implementing deprecated met...

【Swift4】Stringイニシャライズ時のエラー