<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileHandle/acceptConnectionInBackgroundAndNotify()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileHandle(im)acceptConnectionInBackgroundAndNotify"
  },
  "title" : "acceptConnectionInBackgroundAndNotify()"
}
-->

# acceptConnectionInBackgroundAndNotify()

Accepts a socket connection (for stream-type sockets only) in the background and creates a file handle for the “near” (client) end of the communications channel.

```
func acceptConnectionInBackgroundAndNotify()
```

## Discussion

This method asynchronously creates a file handle for the other end of the socket connection and returns that object by posting a [`NSFileHandleConnectionAccepted`](/documentation/Foundation/NSNotification/Name-swift.struct/NSFileHandleConnectionAccepted) notification in the current thread. The notification includes a `userInfo` dictionary with the created `NSFileHandle` object, which is accessible using the `NSFileHandleNotificationFileHandleItem` key.

You must call this method from a thread that has an active run loop.

### Special Considerations

The receiver must be created by an [`init(fileDescriptor:)`](/documentation/Foundation/FileHandle/init(fileDescriptor:)) message that takes as an argument a stream-type socket created by the appropriate system routine, *and that is being listened on*. In other words, you must `bind()` the socket, and ensure that the socket has a connection backlog defined by `listen()`.

The object that will write data to the returned file handle must add itself as an observer of [`NSFileHandleConnectionAccepted`](/documentation/Foundation/NSNotification/Name-swift.struct/NSFileHandleConnectionAccepted).

Note that this method does not continue to listen for connection requests after it posts [`NSFileHandleConnectionAccepted`](/documentation/Foundation/NSNotification/Name-swift.struct/NSFileHandleConnectionAccepted). If you want to keep getting notified, you need to call [`acceptConnectionInBackgroundAndNotify()`](/documentation/Foundation/FileHandle/acceptConnectionInBackgroundAndNotify()) again in your observer method.

## See Also

[`-  enqueueNotification:postingStyle:coalesceMask:forModes:`](/documentation/Foundation/NotificationQueue/enqueue(_:postingStyle:coalesceMask:forModes:))

Adds a notification to the notification queue with a specified posting style, criteria for coalescing, and run loop mode.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)