SwiftUI and UICloudSharingController

Hi there,

I tried already for a while to get the UICloudSharingController implimented in SwiftUI via UIViewControllerRepresentable. I check already in StackOverFlow the following entries: https://stackoverflow.com/questions/59230784/swiftui-and-uicloudsharingcontroller-hate-each-other https://stackoverflow.com/questions/57310673/uicloudsharingcontroller-shows-infinite-activity-indicatory-in-xcode-11

But there are only solutions with an UIButton. I want to get it done with SwiftUI.sheet(). Does anyone has tried it that way? Here is my code in makeUIViewController function from the UIViewControllerRepresentable protocol:

func makeUIViewController(context: UIViewControllerRepresentableContext<UIKitCloudKitSharingViewController>) -> UICloudSharingController {
        var controller : UICloudSharingController!
        
        if let rootRecord = ContentModel.iCloudRecord {
            if let shareThere = rootRecord.share {
                let shareRecordID = shareThere.recordID
                let fetchRecordsOp = CKFetchRecordsOperation(recordIDs: [shareRecordID])
                
                fetchRecordsOp.fetchRecordsCompletionBlock = { recordsByRecordID, error in
                    guard let share = recordsByRecordID?[shareRecordID] as? CKShare else {
                        return
                    }
                    
                    DispatchQueue.main.async {
                        controller = UICloudSharingController(share: share, container: CKContainer.default())
                        controller.availablePermissions = [.allowReadWrite]
                    }
                }
                ContentModel.privateDB.add(fetchRecordsOp)
            } else {
                controller = UICloudSharingController { (_, prepareCompletionHandler) in
                    let operationQueue = OperationQueue()
                    operationQueue.maxConcurrentOperationCount = 1

                    let shareID = CKRecord.ID(recordName: UUID().uuidString, zoneID: CKRecordZone(zoneName: "custom").zoneID)
                    var share = CKShare(rootRecord: rootRecord, shareID: shareID)
                    share[CKShare.SystemFieldKey.title] = "A cool topic to share!" as CKRecordValue
                    share.publicPermission = .readWrite

                    // Save the share. You must save the root record and the CKShare record at the same time.
                    // Use the serverRecord when a partial failure (.serverRecordChanged) occurs.
                    // Let UICloudSharingController handle the other error, until CloudKit calls failedToSaveShareWithError.
                    //
                    let modifyRecordsOp = CKModifyRecordsOperation(recordsToSave: [share, rootRecord], recordIDsToDelete: nil)
                    modifyRecordsOp.modifyRecordsCompletionBlock = { records, recordIDs, error in

                        if let ckError = error as? CKError {
                            if let serverVersion = ckError.serverRecord as? CKShare {
                                share = serverVersion
                            }
                        }
                        prepareCompletionHandler(share, CKContainer.default(), error)
                    }
                    modifyRecordsOp.database = ContentModel.privateDB
                    operationQueue.addOperation(modifyRecordsOp)
                }
            }
        
        return controller
    }
}

As you can see I took the code from Apple "Sharing CloudKit Data with Other iCloud Users". In the initial state of my App there is no CKShare. So the initializer with the preparationHandler is called but got stuck in infinity activity indicator as mentioned in the upper StackOverFlow-Entry. Has anyone an idea how to implemement UICloudSahringController the right way in SwiftUI? Without an extra UIButton but within the SwiftUI-API of .sheet()? Maybe there is out there in the Apple-Community somebody from the Apple-Staff that can give an answer how to implement it correctly? But I appreciate of course every help. In hope to get some answers and if positive - thanks in advance!

Kersten

SwiftUI-API .sheet() should mean a SwiftUI-Button, sorry for this lapse.

Apple offers SwiftUI sample code that you can use in https://github.com/apple/cloudkit-sample-sharing

SwiftUI and UICloudSharingController
 
 
Q