Users can't open CKShare record when they accepted the record and application starts

310 views Asked by At

Working my way thru implementing the CKShare functionality of CloudKit. I Haven manged to get to the part to share a record (via email for example) and also can confirm the user received the invite. The problem is that when user accepts the record then the app pops up but nothing happens. In order to assist here are some key Elements of the app, and please tell me if i am wrong.

1) The application does not require user to login using their Apple ID

2) I am testing the application via a direct built on two different phones (with seperate Apple IDs) when i Connect the phones to the computer with a Cable (aka not using TestFlight yet).

3) I have checked in the CloudkitDashboard and i can see the record that hah been shared and also see that the recored hah been shared, but instead of seeing the user email I sent the invite I see my email and the fact that the record hah been "accepted"

4) I Haven added the CKSharingSupported key in the Info.plist file.

5) The code in the AppDelegate.swift file am using to accept the CKShare is below. I world like to raw your attention to the fact that the string "User Accepted the Share" never gets printed, which makes me think that this part of the code never runs.

func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {

    print("User Accepted the Share")
    let acceptShareOperation: CKAcceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])

    acceptShareOperation.qualityOfService = .userInteractive
    acceptShareOperation.perShareCompletionBlock = {meta, share,
        error in
        print("The Record Share was Accepted")
    }
    acceptShareOperation.acceptSharesCompletionBlock = {
        error in
        /// Send your user to where they need to go in your app
        let viewController: SNPDetailsViewController = self.window?.rootViewController as! SNPDetailsViewController
        viewController.fetchShare(cloudKitShareMetadata)
    }
    CKContainer(identifier:
        cloudKitShareMetadata.containerIdentifier).add(acceptShareOperation)
}

Any insight to guide me where I am wrong, will be much appreciated.

Thank you for your time!

2

There are 2 answers

0
Clifton Labrum On

Please see this answer for more context: CloudKit CKShare URL Goes Nowhere

But make sure that:

  1. You specify a fallback URL for your CloudKit Container that redirects to your application.
  2. Inside your app in Xcode, you set up a URL scheme so that custom URLs like yourapp:// open your application and the query parameter from the fallback URL gets passed into userDidAcceptCloudKitShareWith.
0
DeeNove750 On

After weeks of trial and error, research and LUCK I managed to find out the problem. All tutorials and online solutions relate to the below code in the AppDelegate.swift, to accept a CKShare record:

func application(_ application: UIApplication, userDidAcceptCloudKitShareWith    cloudKitShareMetadata: CKShare.Metadata) {
  let acceptShareOperation: CKAcceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
  acceptShareOperation.qualityOfService = .userInteractive
  acceptShareOperation.perShareCompletionBlock = {meta, share,
        error in
            DispatchQueue.main.async() {
                print("The Record Share was Accepted")
            }
        }
       acceptShareOperation.acceptSharesCompletionBlock = {
        error in
            guard (error == nil) else{
                print("Error \(error?.localizedDescription ?? "")")
                return
            }
        let viewController: SNPDetailsViewController = self.window?.rootViewController as! SNPDetailsViewController
        viewController.fetchShare(cloudKitShareMetadata)
       }
    CKContainer(identifier:
        cloudKitShareMetadata.containerIdentifier).add(acceptShareOperation)
}

The solution in my case was to add it in the SceneDelegate.swift in the following function:

func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
(add above code)
}

Hope this helps others!