CloudKit JavaScript accessing CKAsset URL

29 May 2016

You can use CloudKit JavaScript for accessing url of asset. Here is an example;

(Used Alamofire and SwiftyJSON)


    func testRecordRequest() -> Request {
        let urlString = "https://api.apple-cloudkit.com/database/1/" + Constants.container + "/development/public/records/query?ckAPIToken=" + Constants.cloudKitAPIToken
        let query = ["recordType": "TestRecord"]
        return Alamofire.request(.POST, urlString, parameters: ["query": query], encoding: .JSON, headers: nil)
    }

JSON response contains a “downloadURL” for the asset.


    "downloadURL": "https://cvws.icloud-content.com/B/.../${f}?o=AmVtU..."

”${f}” seems like a variable so change it to anything you like.


    let downloadURLString = json["fields"][Keys.image]["value"]["downloadURL"].stringValue
    let recordName = json["recordName"].stringValue
    let fileName = recordName + ".jpg"
    let imageURLString = downloadURLString.stringByReplacingOccurrencesOfString("${f}", withString: fileName)

And now we can use this urlString to create a NSURL and use it with any image&cache solutions such as Kingfisher, HanekeSwift etc. (You may also want to save image type png/jpg)