访问另一个应用程序的CloudKit数据库?

时间:2017-10-09 20:45:33

标签: ios cloudkit

假设John开发了App A和Heather开发了App B.他们每个人都有不同的Apple Developer帐户,他们不在同一个团队中或以任何方式关联。应用B由公共CloudKit数据库提供支持。 App A有没有办法写入App B的公共CloudKit数据库?也就是说,App A可以这样做:

let DB = CKContainer(identifier: "iCloud.com.Heather.AppB").publicCloudDatabase

然后写入或读取此DB

我猜这是不允许开箱即用的,但有没有办法设置身份验证以便这可能?

2 个答案:

答案 0 :(得分:1)

这看起来/听起来像你寻求的解决方案。

CloudKit share Data between different iCloud accounts but not with everyone由iCloud专家概述

https://stackoverflow.com/users/1878264/edwin-vermeer

此链接也有第三方解释。 https://medium.com/@kwylez/cloudkit-sharing-series-intro-4fc82dad7a9

关键步骤无耻地削减'n'pasted ...确保你在medium.com上阅读和归功于Cory!

// Add an Info.plist key for CloudKit Sharing
 <key>CKSharingSupported</key>
 <true/>

更多代码......

CKContainer.default().discoverUserIdentity(withPhoneNumber: phone, completionHandler: {identity, error in

        guard let userIdentity: CKUserIdentity = identity, error == nil else {

            DispatchQueue.main.async(execute: {
                print("fetch user by phone error " + error!.localizedDescription)
            })

            return
        }

        DispatchQueue.main.async(execute: {
            print("user identity was discovered \(identity)")
        })
    })

///创建一个shred根记录

let recordZone: CKRecordZone = CKRecordZone(zoneName: "FriendZone")
let rootRecord: CKRecord = CKRecord(recordType: "Note", zoneID: recordZone.zoneID)

//创建CloudKit共享记录

let share = CKShare(rootRecord: rootRecord)

share[CKShareTitleKey] = "Shopping List” as CKRecordValue
share[CKShareThumbnailImageDataKey] = shoppingListThumbnail as CKRecordValue
share[CKShareTypeKey] = "com.yourcompany.name" as CKRecordValue

///设置共享的参与者(从你提取的身份中获取CKUserIdentityLookupInfo)

let fetchParticipantsOperation: CKFetchShareParticipantsOperation = CKFetchShareParticipantsOperation(userIdentityLookupInfos: [userIdentity])

fetchParticipantsOperation.fetchShareParticipantsCompletionBlock = {error in

if let error = error {
    print("error for completion" + error!.localizedDescription)
}
}

fetchParticipantsOperation.shareParticipantFetchedBlock = {participant in

print("participant \(participant)")
/// 1
participant.permission = .readWrite

/// 2
share.addParticipant(participant)

let modifyOperation: CKModifyRecordsOperation = CKModifyRecordsOperation(recordsToSave: [rootRecord, share], recordIDsToDelete: nil)

modifyOperation.savePolicy = .ifServerRecordUnchanged
modifyOperation.perRecordCompletionBlock = {record, error in
    print("record completion \(record) and \(error)")
}
modifyOperation.modifyRecordsCompletionBlock = {records, recordIDs, error in

    guard let ckrecords: [CKRecord] = records, let record: CKRecord = ckrecords.first, error == nil else {
        print("error in modifying the records " + error!.localizedDescription)
        return
    }

    /// 3
    print("share url \(url)")
}

CKContainer.default().privateDB.add(modifyOperation)
}

CKContainer.default().add(fetchParticipantsOperation)

在篱笆的另一边。

let acceptShareOperation: CKAcceptSharesOperation =     CKAcceptSharesOperation(shareMetadatas: [shareMeta])
    acceptShareOperation.qualityOfService = .userInteractive
    acceptShareOperation.perShareCompletionBlock = {meta, share, error in
        Log.print("meta \(meta) share \(share) error \(error)")
    }
    acceptShareOperation.acceptSharesCompletionBlock = {error in
        Log.print("error in accept share completion \(error)")
        /// Send your user to wear that need to go in your app
    }

    CKContainer.default().container.add(acceptShareOperation)

真的我不能指望做正义的文章,去读它......它分三部分!

答案 1 :(得分:0)

如果应用程序位于同一组织中,则可以设置共享访问权限。但正如你描述的情况,这是不可能的。