Post

CloudKit 分享功能的必要設定:CKSharingSupported

CloudKit 分享功能的必要設定:CKSharingSupported

1. 問題

CloudKit 的分享功能程式碼都寫好了,UICloudSharingController 也能正常彈出來,但實際分享時不會動作,或是接受分享的那一端根本收不到。


2. 原因

少了一個 Info.plist 的設定:CKSharingSupported

這個 key 告訴系統「這個 app 支援接受 CloudKit 分享」。沒有它的話:

  • 分享連結打開後不會導到你的 app
  • SceneDelegateuserDidAcceptCloudKitShareWith 不會被呼叫
  • 整個分享流程看起來像是壞掉了

3. 解法

在 Info.plist 加上:

1
2
<key>CKSharingSupported</key>
<true/>

4. 其他容易忘記的設定

做 CloudKit 分享功能時,除了程式碼之外,這幾個設定缺一個就會壞:

Info.plist

1
2
3
4
5
6
<key>CKSharingSupported</key>
<true/>
<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

Entitlements

  • com.apple.developer.icloud-servicesCloudKit
  • com.apple.developer.icloud-container-identifiers → 你的 container ID
  • aps-environmentdevelopment(上架時改 production

AppDelegate

必須用 UISceneConfiguration 指定你的 SceneDelegate

1
2
3
4
5
6
7
func application(_ application: UIApplication,
                 configurationForConnecting session: UISceneSession,
                 options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    let config = UISceneConfiguration(name: nil, sessionRole: session.role)
    config.delegateClass = SceneDelegate.self
    return config
}

如果你用的是預設的 @main App struct 而沒有 AppDelegate,就要加 @UIApplicationDelegateAdaptor

1
2
3
4
@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
}

5. 檢查清單

如果分享功能不動,照這個順序檢查:

  1. Info.plist 有沒有 CKSharingSupported = true
  2. Entitlements 有沒有 CloudKit 和正確的 container ID
  3. AppDelegate 有沒有指定 SceneDelegate
  4. SceneDelegate 有沒有實作 userDidAcceptCloudKitShareWith
  5. 你的資料是不是放在自訂 Zone(Default Zone 不能分享)
This post is licensed under CC BY 4.0 by the author.