<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreData",
  "identifier" : "/documentation/CoreData/NSPersistentCloudKitContainer/shareManagedObjects:toShare:completion:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core Data"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPersistentCloudKitContainer(im)shareManagedObjects:toShare:completion:"
  },
  "title" : "shareManagedObjects:toShare:completion:"
}
-->

# shareManagedObjects:toShare:completion:

Associates the specified managed objects with a new or existing share record.

```
- (void) shareManagedObjects:(NSArray<NSManagedObject *> *) managedObjects toShare:(CKShare *) share completion:(void (^)(NSSet<NSManagedObjectID *> *sharedObjectIDs, CKShare *share, CKContainer *cloudKitContainer, NSError *sharingError)) completion;
```

## Parameters

`managedObjects`

The managed objects to share.

`share`

A share record that identifies an existing shared record zone to associate the managed objects with. Use `nil` to create a new shared record zone that contains only the specified managed objects and doesn’t have any participants.

`completion`

The handler to invoke after Core Data shares the managed objects.

## Discussion

The `completion` closure returns no value and takes the following parameters:

- The IDs of the shared objects.
- The share record. For more information, see <doc://com.apple.documentation/documentation/CloudKit/CKShare>.
- The CloudKit container of the database that stores the shared record zone. For more information, see <doc://com.apple.documentation/documentation/CloudKit/CKContainer>.
- An error object that contains information about a problem, or `nil` if the managed objects share successfully.

When you invoke this method, Core Data performs a deep traversal among the specified managed objects and adds any related objects to the share.

Sharing fails if any of the following conditions apply:

- Any objects in `managedObjects`, or those the traversal finds, belong to an existing share record.
- Any objects in `managedObjects` belong to a persistent store that doesn’t support sharing, such as those you configure with the <doc://com.apple.documentation/documentation/CloudKit/CKDatabase/Scope/public> database scope.
- The current device state doesn’t support sharing, such as when there isn’t an active iCloud account.

You can use `completion` directly with the <doc://com.apple.documentation/documentation/UIKit/UICloudSharingController/init(preparationHandler:)> method of <doc://com.apple.documentation/documentation/UIKit/UICloudSharingController>, as the following example shows.

```objc
- (void)shareObjects:(NSArray *)objects
         inContainer:(NSPersistentCloudKitContainer *)container {
    
    // Create an instance of UICloudSharingController.
    UICloudSharingController *cloudSharingController =
        [[UICloudSharingController alloc]
         initWithPreparationHandler:^(UICloudSharingController * _Nonnull controller,
                                      void (^ _Nonnull preparationCompletionHandler)(
                                       CKShare * _Nullable,
                                       CKContainer * _Nullable,
                                       NSError * _Nullable)) {
        
        // Call the container's share method, passing the managed objects
        // to share.
        [container shareManagedObjects:objects
                               toShare:NULL
                            completion:^(NSSet<NSManagedObjectID *>
                                         * _Nullable sharedObjectIDs,
                                         CKShare * _Nullable share,
                                         CKContainer * _Nullable cloudKitContainer,
                                         NSError * _Nullable sharingError) {
            
            // Invoke the cloud-sharing controller's completion handler,
            // passing the share, container, and error parameters that
            // NSPersistentCloudKitContainer provides.
            preparationCompletionHandler(share, cloudKitContainer, sharingError);
        }];
    }];
    
    // Present the cloud-sharing controller to the user.
    [self presentViewController:cloudSharingController
                       animated:true
                     completion:NULL];
}
```

---

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)