CloudKit CKShare URL无处可去 [英] CloudKit CKShare URL Goes Nowhere

查看:37
本文介绍了CloudKit CKShare URL无处可去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地将 CKShare URL保存到CloudKit,并且可以看到用户在CloudKit仪表板中已 INVITED .我的Mac应用通过电子邮件将URL发送给该人,但是当他们单击该URL时,他们在icloud.com的该屏幕上都可以看到它:

I have successfully saved a CKShare URL to CloudKit, and I can see that the user is INVITED in the CloudKit Dashboard. My Mac app emailed the URL to that person, but when they click it, all they see it this screen on icloud.com:

点击确定会使所有内容消失,因此您所看到的只是网页的背景.

Clicking OK makes everything disappear so all you see is the background on the web page.

我的理解是,该URL应该在我的Mac应用中打开,它将在我的应用委托中触发 userDidAcceptCloudKitShareWith .却什么也没做.

My understanding is that the URL is supposed to open my Mac app where it will fire userDidAcceptCloudKitShareWith in my app delegate. But it does nothing.

这可能是因为我的应用程序正在开发中,而不是在Mac App Store中吗?我是否需要自定义URL方案才能使其打开我的应用?

Could this be because my app is in development and not in the Mac App Store yet? Do I need a custom URL scheme to get it to open my app?

有关此资料的文档非常少.我希望有人能提供任何帮助.

Documentation on this stuff is pretty sparse. I'd love any help someone can provide.

推荐答案

此后,我了解到必须为CloudKit容器指定一个备用URL.如果未安装该应用程序(或未被识别,例如像我一样在Xcode中进行开发构建时,情况似乎如此),CloudKit会将共享URL转发到您指定的位置.他们将唯一的共享ID附加到URL,以便您可以在自己的网页上对其进行处理.

I have since learned that you must specify a fallback URL for your CloudKit container. In cases where the app isn't installed (or isn't recognized, which seems to be the case when doing dev builds in Xcode like I am), CloudKit will forward share URL to somewhere you specify. They append the unique share ID to the URL so that you can process it on your own web page.

在CloudKit仪表板中,转到环境设置... ,您将看到此弹出窗口:

In the CloudKit dashboard, go to Environment Settings... and you'll see this popup:

我将其重定向到 https://myapp.com/share/?id= ,并在重定向到的网页上执行 $ _ GET ['id'] 来获取 id .然后,我使用自定义URL方案再次重定向到我的应用程序,并传递共享ID(例如, myapp://abc123 ,其中 abc123 是共享ID).

I have it redirect to https://myapp.com/share/?id= and on my web page where it redirects to, I do a $_GET['id'] to grab the id. I then do another redirect to my application using a custom URL scheme and pass the share ID (e.g. myapp://abc123 where abc123 is the share ID).

在我的应用程序委托中,我收到这样的URL:

In my app delegate, I receive the URL like this:

func application(_ application: NSApplication, open urls: [URL]) {
  if let url = urls.first, let shareId = url.host{
    fetchShare(shareId) //<-- sharedId = abc123
  }
}

然后我使用 CKFetchShareMetadataOperation 查找共享的URL,并使用 CKAcceptSharesOperation 接受它,如下所示:

I then use CKFetchShareMetadataOperation to look up the URL of the share and CKAcceptSharesOperation to accept it like this:

func fetchShare(shareId: String){
  if let url = URL(string: "https://www.icloud.com/share/\(shareId)"){
    let operation = CKFetchShareMetadataOperation(shareURLs: [url])

    operation.perShareMetadataBlock = { url, metadata, error in
      if let metadata = metadata{
        //:::
        acceptShare(metadata: metadata)
      }
    }
    operation.fetchShareMetadataCompletionBlock = { error in
      if let error = error{
        print("fetch Share error: \(error)")
      }
    }
    CKContainer.default().add(operation)
  }
}

func acceptShare(metadata: CKShareMetadata){
  let operation = CKAcceptSharesOperation(shareMetadatas: [metadata])

  operation.acceptSharesCompletionBlock = { error in
    if let error = error{
      print("accept share error: \(error)")
    }else{
      //Share accepted!
    }
  }
  CKContainer.default().add(operation)
}

我认为,使用 NSItemProvider NSSharingService 可以更轻松地完成此操作,但是我正在做很多自定义UI,并且希望完全控制共享工作流程.

I think there are easier ways to work through this using NSItemProvider and NSSharingService, but I'm doing a lot of custom UI and wanted to have full control of the share workflow.

我希望这对某人有帮助.:)

I hope this helps someone. :)

这篇关于CloudKit CKShare URL无处可去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆