URLSession: Operation not permitted

I'm trying to do a simple POST request in Swift in my Safari Extension, but there is no way that it works.

Example code:


let url = URL(string: "https://136.243.65.157")
  let task = URLSession.shared.dataTask(with: url! as URL) { data, response, error in
    guard let data = data, error == nil else { return }
    print(NSString(data: data, encoding: String.Encoding.utf8.rawValue))
  }
  task.resume()


I get:

2019-10-19 22:03:14.430621+0200 TrashMail.com Extension[16213:361162] [] nw_socket_connect [C2:3] connectx(12 (guarded), [srcif=0, srcaddr=, dstaddr=136.243.65.157:443], SAE_ASSOCID_ANY, 0, NULL, 0, NULL, SAE_CONNID_ANY) failed: [1: Operation not permitted]
2019-10-19 22:03:14.430772+0200 TrashMail.com Extension[16213:361162] [] nw_socket_connect [C2:3] connectx failed (fd 12) [1: Operation not permitted]
2019-10-19 22:03:14.430851+0200 TrashMail.com Extension[16213:361162] [] nw_socket_connect connectx failed [1: Operation not permitted]
2019-10-19 22:03:14.431318+0200 TrashMail.com Extension[16213:361162] Connection 2: received failure notification
2019-10-19 22:03:14.431363+0200 TrashMail.com Extension[16213:361162] Connection 2: failed to connect 1:1, reason -1
2019-10-19 22:03:14.431388+0200 TrashMail.com Extension[16213:361162] Connection 2: encountered error(1:1)
2019-10-19 22:03:14.432825+0200 TrashMail.com Extension[16213:361162] Task <34726962-7BF0-4ED5-8DD2-32E4A4DDD1B3>.<2> HTTP load failed, 0/0 bytes (error code: 1 [1:1])
2019-10-19 22:03:14.433103+0200 TrashMail.com Extension[16213:361213] Task <34726962-7BF0-4ED5-8DD2-32E4A4DDD1B3>.<2> finished with error [1] Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_kCFStreamErrorCodeKey=1, _kCFStreamErrorDomainKey=1}

Is it possible that I have to do this in JavaScript instead Swift4?

Accepted Reply

Does your extension have permission to send outgoing network requests? You would specify this in the App Sandbox profile for your extension.


Check out https://developer.apple.com/documentation/xcode/adding_capabilities_to_your_app?language=objc


But you would want to do this for the extension, not for the app.

Replies

Does your extension have permission to send outgoing network requests? You would specify this in the App Sandbox profile for your extension.


Check out https://developer.apple.com/documentation/xcode/adding_capabilities_to_your_app?language=objc


But you would want to do this for the extension, not for the app.

I fixed the problem by adding in my extension ".entitlements" file the entry "com.apple.security.network.client".

I found now out with your guide, that its possible to do the same with the GUI your provided in the link.