无法在保留现有 iCloud 文档的同时实施 iCloud Drive/Files 支持。 $(TeamIdentifierPrefix)

Unable to implement iCloud Drive/Files support whilst keeping existing iCloud documents. $(TeamIdentifierPrefix)

如标题所示,我想在现有应用程序上实现 iCloud Drive 支持,其中无处不在的容器以旧样式 $(TeamIdentifierPrefix) 为前缀。对于我的生活,我无法让它发挥作用。

最初我尝试了 Apple 自己的步骤,详见此处:https://developer.apple.com/library/content/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForDocumentsIniCloud.html#//apple_ref/doc/uid/TP40012094-CH2-SW20

不幸的是,这没有用。 (我记得每次构建都要增加 Bundle Version)

按照此 post 上可用的步骤:iCloud Drive Folder,我能够让我的应用程序出现在 iCloud Drive 中,但是我现有的文档无法访问,因为它们位于例如X123F4563T8.com.example.app 而不是新创建的 iCloud.com.example.app.

我尝试了无数种不同的标识符配置,试图让 iCloud Drive 指向旧的无处不在的容器,但无济于事。

我的权利文件如下所示(带有我的应用程序自己的标识符):

<key>com.apple.developer.icloud-container-identifiers</key>
<array>
    <string>iCloud.com.example.AppName</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
    <string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
    <string>iCloud.com.example.AppName</string>
</array>
<key>keychain-access-groups</key>
<array>
    <string>$(AppIdentifierPrefix)com.example.AppName</string>
</array>

第一个关键似乎是决定使用哪个无处不在的目录。

之前设置为:

<key>com.apple.developer.icloud-container-identifiers</key>
<array>
    <string>$(TeamIdentifierPrefix)com.example.AppName</string>
</array>

我缩小了搜索范围,发现这是该键的值,这似乎是决定是否使用(旧式)iCloud 文档的决定因素

 $(TeamIdentifierPrefix)com.example.AppName

或 iCloud 云盘,

 iCloud.com.example.AppName

为了完整起见,以下内容包含在我的 Info.plist 中:

<key>iCloud.com.example.AppName</key>
<dict>
    <key>NSUbiquitousContainerIsDocumentScopePublic</key>
    <true/>
    <key>NSUbiquitousContainerName</key>
    <string>App Name For Drive</string>
    <key>NSUbiquitousContainerSupportedFolderLevels</key>
    <string>Any</string>
</dict>

我也没有忽略:

<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
    <string>iCloud.com.example.AppName</string>
</array>

不过这个值好像对结果没有影响。

为了让它正常工作,我的 Info.plist 添加了以下条目:

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.Example.AppName</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>Example App Name</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

我的 AppName.entitlements 文件包含其中:

<key>com.apple.developer.icloud-container-identifiers</key>
<array>
    <string>$(TeamIdentifierPrefix)com.example.AppName</string>
    <string>iCloud.com.example.AppName</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
    <string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
    <string>$(TeamIdentifierPrefix)com.example.AppName</string>
</array>

既然我们都设置了访问这两个容器的能力,我将我希望能够访问的 2 个 iCloud 无处不在的地址(旧式 iCloud 文档和更新的 iCloud Drive)添加到我的 Info.plist:

<key>UbiquitousContainers</key>
<array>
    <string>$(TeamIdentifierPrefix)com.example.AppName</string>
    <string>iCloud.com.example.AppName</string>
</array>

然后我将以下内容添加到常量文件中以便于访问:

struct UbiquityContainers {
    private static let ubiquityContainers : [String] = Bundle.main.object(forInfoDictionaryKey: "UbiquitousContainers") as! [String]
    static var iCloudDocuments : String {
        get {
            return ubiquityContainers[0]
        }
    }
    static var iCloudDrive : String {
        get {
            return ubiquityContainers[1]
        }
    }
}

我选择允许用户通过 TabBar 在 2 个容器之间切换。旧的 iCloud Documents 目录包含我应用程序旧版本的文档,在打开之前需要将其转换为新的文档格式,因此在我的情况下,为每个文件版本提供单独的视图是有意义的。

var ubiquityContainer : String = UbiquityContainers.iCloudDrive
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(self.documentsInCloud) {
        if(item == self.iCloudDriveTabBarItem) {
            self.ubiquityContainer = UbiquityContainers.iCloudDrive
        } else if(item == self.iCloudDocumentsTabBarItem) {
            self.ubiquityContainer = UbiquityContainers.iCloudDocuments
        }
        self.startCloudQuery()
    }
}

我希望这对将来可能因毛囊散落在地上而缺乏毛囊的人有所帮助。 :)