Watchkit 扩展无法从 iCloud 读取

Watchkit Extension Cannot Read from iCloud

我正在寻找用于从 iCloud 文档存储中读取文件的 watchkit 扩展。我从 iOS 应用程序中编写了该文件,该应用程序也能够读取它。我正在利用共享 class 所以代码是相同的。问题是,在手表上,云容器 returns 的 URL 为零。

static func readFromFile(fileName:String) -> String?
{
    let fileManager = FileManager.default
    var cloudURL = fileManager.url(forUbiquityContainerIdentifier: nil)

    cloudURL = cloudURL?.appendingPathComponent("Documents/\(fileName)")

    do
    {
        return try String(contentsOf: cloudURL!)
    } catch {
        print (error)
        return nil
    }

在上面的例子中,cloudURL 在手表上是 nil 而不是 phone。对于 forUbiquityContainerIdentifier,我检查了 watch 和 phone 是否使用了相同的标识符。我还尝试直接输入名称而不是使用 nil 并让它从权利中获取它。容器格式:

iCloud.com.company.app

据我了解,Watch OS 3 应该可以使用 iCloud。

更新:

我打印出 let token = fileManager.ubiquityIdentityToken 并得到以下信息:

WatchKit Extension[390:687462] [default] [ERROR] error while getting ubiquityIdentityToken: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.bird.token was invalidated." UserInfo={NSDebugDescription=The connection to service named com.apple.bird.token was invalidated.}

我已从 Apple 正式收到回复,称这不是 Watch OS 支持的功能。这与我让他们知道的他们自己的文档背道而驰。希望其他人看到这个回复,它可以节省我浪费的大量时间。

当时是 2021 年。Apple 文档仍然说 watchOS 2.0+ 可以使用 iCloud Drive 方法,但是 url forUbiquityContainerIdentifier returns nil :(