Sample code for 30+ languages & platforms
Swift

Bunny Sign URL and then Download using Signed URL

See more Bunny CDN Examples

Shows how to sign a URL for BunnyCDN Token Authentication and then use it to download.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    var mySecurityKey: String? = "e7ea8d73-8fa0-44ef-a2cc-91526f7df5ed"

    var url: String? = "https://test.b-cdn.net/sample-pdf-with-images.pdf"

    // Extract the URL components.
    let urlObj = CkoUrl()!
    urlObj.parse(url: url)

    var url_scheme: String? = "https"
    if urlObj.ssl == false {
        url_scheme = "http"
    }

    var url_host: String? = urlObj.host
    var url_path: String? = urlObj.path

    // Calculate an expiration time 1 hour from the current date/time.
    let expTime = CkoDateTime()!
    expTime.setFromCurrentSystemTime()
    expTime.addSeconds(numSeconds: 3600)
    var expires: String? = expTime.get(asUnixTimeStr: false)

    print("Expires = \(expires!)")

    // Create the string to hash
    let sbToHash = CkoStringBuilder()!
    sbToHash.append(value: mySecurityKey)
    sbToHash.append(value: url_path)
    sbToHash.append(value: expires)

    // Base64Url encoding is the same as base64, except "-" is used instead of "+",
    // "_" is used instead of "/", and no "=" padding is added.
    var token: String? = sbToHash.getHash(algorithm: "sha256", encoding: "base64Url", charset: "utf-8")

    let sbSignedUrl = CkoStringBuilder()!
    sbSignedUrl.append(value: url_scheme)
    sbSignedUrl.append(value: "://")
    sbSignedUrl.append(value: url_host)
    sbSignedUrl.append(value: url_path)
    sbSignedUrl.append(value: "?token=")
    sbSignedUrl.append(value: token)
    sbSignedUrl.append(value: "&expires=")
    sbSignedUrl.append(value: expires)

    var signedUrl: String? = sbSignedUrl.getAsString()
    print("Signed URL: \(signedUrl!)")

    // Use the signed URL to download the file.
    let http = CkoHttp()!
    success = http.download(url: signedUrl, saveToPath: "c:/aaworkarea/sample.pdf")
    if success == false {
        print("\(http.lastErrorText!)")
    }
    else {
        print("Success.")
    }


}