Find answers, guides, and tutorials to supercharge your content delivery.

Swift CDN Integration

Published on February 12, 2019

Swift is a programming language released in 2014 and is developed by Apple Inc. primarily for devices using Apple's operating systems, such as iOS, macOS, watchOS, and tvOS. It is a multi-paradigm language that is intended to work with Apple's Cocoa and Cocoa Touch frameworks as well as Objective-C code.

How to complete a Swift CDN integration

This integration guide demonstrates how to complete a Swift CDN integration with KeyCDN. Follow the steps below to successfully integrate your Swift application with KeyCDN:

  1. Create a Pull Zone for the origin server (e.g. Linode, AWS S3, or DigitalOcean Spaces). Alternatively use a Push Zone as an origin storage server.
  2. Use the Zone URL (e.g. instead of the origin URL when making requests. For example, when downloading an image you could use one of the following functions or extension from Leo Dabus for Swift 3 or later:
    1. Synchronously:

      if let filePath = Bundle.main.path(forResource: "imageName", ofType: "png"), let image = UIImage(contentsOfFile: filePath) {
          imageView.contentMode = .scaleAspectFit
          imageView.image = image


      // Create a method with a completion handler to get the image data from the URL
      func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
          URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
      // Create a method to download the image by starting the task
      func downloadImage(from url: URL) {
          print("Download Started")
          getData(from: url) { data, response, error in
              guard let data = data, error == nil else { return }
              print(response?.suggestedFilename ?? url.lastPathComponent)
              print("Download Finished")
              DispatchQueue.main.async() {
                  self.imageView.image = UIImage(data: data)


      override func viewDidLoad() {
          // Do any additional setup after loading the view
          print("Begin of code")
          if let url = URL(string: "") {
              imageView.contentMode = .scaleAspectFit
              downloadImage(from: url)
          print("End of code. The image will continue downloading in the background and it will be loaded when it ends.")
    2. Extension:

      extension UIImageView {
          func downloaded(from url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {  // for Swift 4.2 syntax just use => mode: UIViewContentMode
              contentMode = mode
              URLSession.shared.dataTask(with: url) { data, response, error in
                      let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                      let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                      let data = data, error == nil,
                      let image = UIImage(data: data)
                      else { return }
                  DispatchQueue.main.async() {
                      self.image = image
          func downloaded(from link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {  // for Swift 4.2 syntax just use => mode: UIViewContentMode
              guard let url = URL(string: link) else { return }
              downloaded(from: url, contentMode: mode)


      imageView.downloaded(from: "")
      Note: You can use a custom subdomain by creating a Zone Alias.

Upon completing this Swift CDN integration, your Swift application will deliver requested content from our global network. This will significantly improve the performance and user experience of your Swift application. The same principle can be applied to many other content types, such as HTML documents, on demand or live stream video, and audio. Adding a CDN to your Swift application will bring your content as close as possible to your users, decreasing the overall latency of your application.

Do you know of another integration approach with Swift that we should mention here? Contact us to let us know and we will add it.

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more