<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CloudKit",
  "identifier" : "/documentation/CloudKit/CKAcceptSharesOperation",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "CloudKit"
    ],
    "preciseIdentifier" : "c:objc(cs)CKAcceptSharesOperation"
  },
  "title" : "CKAcceptSharesOperation"
}
-->

# CKAcceptSharesOperation

An operation that confirms a user’s participation in a share.

```
class CKAcceptSharesOperation
```

## Overview

Use this operation to accept participation in one or more shares. You create the operation with an array of share metadatas, which CloudKit provides to your app when the user taps or clicks a share’s [`url`](/documentation/CloudKit/CKShare/url). The method CloudKit calls varies by platform and app configuration. For more information, see [`CKShare.Metadata`](/documentation/CloudKit/CKShare/Metadata). You can also fetch a share’s metadata using [`CKFetchShareMetadataOperation`](/documentation/CloudKit/CKFetchShareMetadataOperation).

If there are several metadatas, group them by their [`containerIdentifier`](/documentation/CloudKit/CKShare/Metadata/containerIdentifier) and create an operation for each container. Then add the operation to each container’s operation queue to run it. The operation executes its callbacks on a private serial queue.

The operation calls [`perShareCompletionBlock`](/documentation/CloudKit/CKAcceptSharesOperation/perShareCompletionBlock) once for each metadata you provide. CloudKit returns the metadata and its related share, or an error if it can’t accept the share. CloudKit also batches per-metadata errors. If the operation completes with errors, it returns a [`partialFailure`](/documentation/CloudKit/CKError/partialFailure) error. The error stores individual errors in its <doc://com.apple.documentation/documentation/Foundation/NSError/userInfo> dictionary. Use the [`CKPartialErrorsByItemIDKey`](/documentation/CloudKit/CKPartialErrorsByItemIDKey) key to extract them.

After CloudKit applies all record changes, the operation calls [`acceptSharesCompletionBlock`](/documentation/CloudKit/CKAcceptSharesOperation/acceptSharesCompletionBlock). When the closure executes, the server may continue processing residual tasks of the operation, such as creating the record zone in the user’s private database.

The following example demonstrates how to accept a share that CloudKit provides to your window scene delegate. It shows how to create the operation, configure it, and execute it in the correct container:

```swift
func windowScene(_ windowScene: UIWindowScene,
    userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {

    // Accept the share. If successful, schedule a fetch of the
    // share's root record.
    acceptShare(metadata: cloudKitShareMetadata) { [weak self] result in
        switch result {
        case .success(let recordID):
            self?.fetchRootRecordAndNotifyObservers(recordID)
        case .failure(let error):
            // Handle the error...
        }
    }
}

func acceptShare(metadata: CKShare.Metadata,
    completion: @escaping (Result<CKRecord.ID, any Error>) -> Void) {

    // Create a reference to the share's container so the operation
    // executes in the correct context.
    let container = CKContainer(identifier: metadata.containerIdentifier)

    // Create the operation using the metadata the caller provides.
    let operation = CKAcceptSharesOperation(shareMetadatas: [metadata])

    var rootRecordID: CKRecord.ID!
    // If CloudKit accepts the share, cache the root record's ID.
    // The completion closure handles any errors.
    operation.perShareCompletionBlock = { metadata, share, error in
        if let _ = share, error == nil {
            rootRecordID = hierarchicalRootRecordID
        }
    }

    // If the operation fails, return the error to the caller.
    // Otherwise, return the record ID of the share's root record.
    operation.acceptSharesCompletionBlock = { error in
        if let error = error {
            completion(.failure(error))
        } else {
            completion(.success(rootRecordID))
        }
    }

    // Set an appropriate QoS and add the operation to the
    // container's queue to execute it.
    operation.qualityOfService = .utility
    container.add(operation)
}
```

## Topics

### Creating a Share Accept Operation

[`init()`](/documentation/CloudKit/CKAcceptSharesOperation/init())

Creates an operation for accepting shares.

[`init(shareMetadatas:)`](/documentation/CloudKit/CKAcceptSharesOperation/init(shareMetadatas:))

Creates an operation for accepting the specified shares.

### Processing the Share Accept Results

[`shareMetadatas`](/documentation/CloudKit/CKAcceptSharesOperation/shareMetadatas)

The share metadatas to process.

[`perShareCompletionBlock`](/documentation/CloudKit/CKAcceptSharesOperation/perShareCompletionBlock)

The block to execute as CloudKit processes individual shares.

[`acceptSharesCompletionBlock`](/documentation/CloudKit/CKAcceptSharesOperation/acceptSharesCompletionBlock)

The closure to execute when the operation finishes.



---

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)