使用CloudKit从iCloud获取公共记录 [英] Fetching Public Record From iCloud Using CloudKit
问题描述
我有以下代码将我的资产上传到iCloud
I have the following code for uploading my assets to 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)
}
}
}
因为我能够进入CloudKit仪表板,然后实际查看记录并下载其附件。
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.
以下是该函数输出的saveRecord的示例:
Here is an example of the savedRecord that is output from that function:
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>>]
我认为,由于这是公共记录,我可以在共享相同自定义iCloud容器字符串的另一个应用程序中下载此文件。
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.
但是,使用RecordIDName(在这种情况下:F6DF33CB-35DA-483A -A5B5-33542E435689),我无法提取记录。
However, using the RecordIDName (in this case: F6DF33CB-35DA-483A-A5B5-33542E435689), I am unable to fetch the record.
这是我下载它的代码:
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)
}
}
}
}
我只是得到如下错误:
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! :)
推荐答案
错误在于使用
CKContainer.default()
在创建和提取阶段。相反,我应该在其中使用指定的自定义容器,即
In both the creation and pulling stages. Instead, I should have used a specified custom container inside, i.e.
CKContainer(identifier: "iCloud.tech.jackrobson.myApp")
这篇关于使用CloudKit从iCloud获取公共记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!