The Canva editor has a Publish button. When a user clicks this button, the iOS SDK provides your app with:
- a URL for downloading the user's design as a PNG file
- the ID of the user's design
This part of the tutorial series explains how to download and render the user's published design.
Step 1: Set up the UI
-
Open the
Main.storyboard
file. -
Add a Image View to the UI.
-
Create an outlet for the Image View:
@IBOutlet weak var publishedImageView: UIImageView!swift
Step 2: Download (and render) the image
The didPublishDesignWithDesignID
method runs when a user publishes their design via the Canva editor. It receives a url
argument, which your app can use to download the published design.
The following snippet demonstrates how to download a user's design and render it in an Image View:
func canvaViewController(_ canvaViewController: CanvaViewController, didPublishDesignWithDesignID designID: String?, url: URL?) {print("Published a design with an ID of \(designID ?? "nil").")// Download and render the user's imageguard let url = url else { return }let task = URLSession.shared.dataTask(with: url) { data, urlResponse, error inguard let downloadedImage = data,let imageData = Data(base64Encoded: downloadedImage.base64EncodedString())else { return }DispatchQueue.main.async {self.publishedImageView.image = UIImage(data: imageData)}}task.resume()}
swift
Example
ViewController.swift
import UIKitimport CanvaButtonclass ViewController: UIViewController {@IBOutlet weak var publishedImageView: UIImageView!override func viewDidLoad() {super.viewDidLoad()}@IBAction func createDesign(_ sender: Any) {do {let configuration = CanvaViewController.Configuration(apiKey: "API KEY GOES HERE")let canvaViewController = try CanvaViewController(deisgnType: "BookCover", configuration: configuration)canvaViewController.delegate = selfself.present(canvaViewController, animated: true)} catch {print("Something went wrong. Unable to create a new design.")print(error)}}}extension ViewController: CanvaViewControllerDelegate {func canvaViewController(_ canvaViewController: CanvaViewController, didFailToLoadWithError error: Error) {print("Unable to load Canva editor.")print(error)}func canvaViewController(_ canvaViewController: CanvaViewController, didTerminateLoadingForDesignID designID: String?) {print("Unable to open design with an ID of \(designID ?? "nil").")}func canvaViewController(_ canvaViewController: CanvaViewController, didPublishDesignWithDesignID designID: String?, url: URL?) {print("Published a design with an ID of \(designID ?? "nil").")// Download and render the user's imageguard let url = url else { return }let task = URLSession.shared.dataTask(with: url) { data, urlResponse, error inguard let downloadedImage = data,let imageData = Data(base64Encoded: downloadedImage.base64EncodedString())else { return }DispatchQueue.main.async {self.publishedImageView.image = UIImage(data: imageData)}}task.resume()}func canvaViewDidFailPublish(forDesignID designID: String?, withError error: Error) {print("Unable to publish design with an ID of \(designID ?? "nil").")print(error)}func canvaViewController(_ canvaViewController: CanvaViewController, didFinishLoadingWithDesignID designID: String) {print("Opened a design with an ID of \(designID).")}}
swift