简体   繁体   English

使用CloudKit从iCloud获取公共记录

[英]Fetching Public Record From iCloud Using CloudKit

I have the following code for uploading my assets to iCloud 我有以下代码将我的资产上传到iCloud

func uploadAsset(recordType: String, fileURL: URL) {
    let record = CKRecord(recordType: recordType)
    record["file"] = CKAsset(fileURL: fileURL)

    CKContainer.default().publicCloudDatabase.save(record) { savedRecord, error in
        if let saveError = error {
            print("An error occurred in \(saveError)")
        } else {
            print(savedRecord)
        }
    }
}

It definitely works because I am able to go onto the CloudKit dashboard, and physically see the records as well as download their attached files. 它绝对有效,因为我能够进入CloudKit仪表板,并实际查看记录并下载其附件。

Here is an example of the savedRecord that is output from that function: 这是从该函数输出的saveRecord的示例:

Optional(<CKRecord: 0x100f27f60; recordID=F6DF33CB-35DA-483A-A5B5-33542E435689:(_defaultZone:__defaultOwner__), recordChangeTag=izfmgx06, values={
    file = "<CKAsset: 0x1703e6800; path=/private/var/mobile/Containers/Shared/AppGroup/992A6367-11F5-491F-BE37-C9E4BBA865E6/voice/1487611173.95385.m4a, size=33547, UUID=D8F70489-1FF8-4CE8-99BE-390741E8136D, signature=<01a5bd3c fd9b8042 cb52cc23 3681af91 50aacaa5 97>>";
}, recordType=Voice>
{
    creatorUserRecordID -> <CKRecordID: 0x17003ebe0; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    lastModifiedUserRecordID -> <CKRecordID: 0x17003e740; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    creationDate -> 2017-02-21 14:24:28 +0000
    modificationDate -> 2017-02-21 14:24:28 +0000
    modifiedByDevice -> 3AB140FD824827C374412EBB6B72307C57DCF1E07A7FE58F13EBF445ACCB0EA3
    file -> <CKAsset: 0x1703e6800; path=/private/var/mobile/Containers/Shared/AppGroup/992A6367-11F5-491F-BE37-C9E4BBA865E6/voice/1487611173.95385.m4a, size=33547, UUID=D8F70489-1FF8-4CE8-99BE-390741E8136D, signature=<01a5bd3c fd9b8042 cb52cc23 3681af91 50aacaa5 97>>]

I thought, due to this being a public record, I'd be able to download this inside another app that shares the same custom iCloud container string. 我认为,由于这是公共记录,因此我可以在共享相同自定义iCloud容器字符串的另一个应用程序中下载此文件。

However, using the RecordIDName (in this case: F6DF33CB-35DA-483A-A5B5-33542E435689), I am unable to fetch the record. 但是,使用RecordIDName(在这种情况下:F6DF33CB-35DA-483A-A5B5-33542E435689),我无法提取记录。

This is my code for downloading it: 这是我下载它的代码:

    let recordId = CKRecordID(recordName: "F6DF33CB-35DA-483A-A5B5-33542E435689")


    CKContainer.default().publicCloudDatabase.fetch(withRecordID: recordId) { (results, error) -> Void in

        DispatchQueue.main.async {
            if error != nil {

                print(" Error Fetching Record  " + error!.localizedDescription)
            } else {
                if results != nil {
                    print("pulled record")

                    let record = results as CKRecord!

                    print(record)
                }
            }
        }
    }

I simply get the error as follows: 我只是得到如下错误:

 Error Fetching Record  Error fetching record <CKRecordID: 0x10024c6f0; recordName=6988103A-E795-441B-A115-2D044872AAAC, zoneID=_defaultZone:__defaultOwner__> from server: Record not found

Any help appreciated. 任何帮助表示赞赏。 Thanks in advance! 提前致谢! :) :)

The error was in using 错误是在使用

CKContainer.default()

In both the creation and pulling stages. 在创建和拉动阶段。 Instead, I should have used a specified custom container inside, ie 相反,我应该在内部使用一个指定的自定义容器,即

CKContainer(identifier: "iCloud.tech.jackrobson.myApp")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM