7

I am playing with CocoaAsyncSocket in Swift to bind to a UDP socket and receive messages over the local network.

I am initialising a socket, and trying to bind to a port but am getting a NSPOSIXErrorDomain error. Perhaps indicating some sort of permissions issue?

My code:

import Cocoa
import CocoaAsyncSocket

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, GCDAsyncUdpSocketDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let socket = GCDAsyncUdpSocket.init(delegate: self, delegateQueue: DispatchQueue.main)
        do {
            try socket.bind(toPort: 53401)
        } catch let msg {
            NSLog("Error....\(msg)")
        }
    }
}

Full error:

Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedDescription=Operation not permitted, NSLocalizedFailureReason=Error in bind() function}
james246
  • 1,884
  • 1
  • 15
  • 28
  • I have the same issue (in Objective-C). The strange thing is that when I run the `UdpEchoServer` sample app supplied with the project, the same bind call works correctly. – Jan Jan 12 '18 at 10:51

2 Answers2

7

You should enable its network capability

in macOS Catalina Version 10.15.3:

enter image description here

black_pearl
  • 2,549
  • 1
  • 23
  • 36
5

I believe it's the generated Xcode entitlements that prevent from binding. I changed those values to false and now the bind works

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <false/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <false/>
</dict>
</plist>
Jan
  • 7,444
  • 9
  • 50
  • 74
  • AFAIK this disables sandboxing for the macOS App. Apple says in it's Sandbox Guidelines "Apps distributed through the Mac App Store must adopt App Sandbox". So with this solution it would not be possible anymore to publish the app via Mac App Store - is this true? – Dominik Seemayr Jun 26 '19 at 07:44
  • I use this macOS app only for local development without publishing to the AppStore so I don't know – Jan Jun 26 '19 at 08:19