Skip to content

Instantly share code, notes, and snippets.

@MarcoEidinger
Last active September 4, 2021 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoEidinger/24277a831a72bed1934b7e250208d2e3 to your computer and use it in GitHub Desktop.
Save MarcoEidinger/24277a831a72bed1934b7e250208d2e3 to your computer and use it in GitHub Desktop.
Gists for "Rich and interactive cards in SwiftUI" blog post. Original: https://github.com/MarcoEidinger/ms-adaptivecards-ios-example
import Foundation
import UIKit
import SafariServices
class AdaptiveCardViewController: UIViewController, ACRActionDelegate{
internal var cardJson: String = "serialized Adaptive Card JSON to be set"
override func viewDidLoad() {
super.viewDidLoad()
let cardParseResult = ACOAdaptiveCard.fromJson(cardJson);
if((cardParseResult?.isValid)!){
let renderResult = ACRRenderer.render(cardParseResult!.card, config: nil, widthConstraint: 335);
if(renderResult?.succeeded ?? false)
{
let ad = renderResult?.view;
ad!.acrActionDelegate = (self as ACRActionDelegate);
self.view.autoresizingMask = [.flexibleHeight];
self.view.addSubview(ad!);
ad!.translatesAutoresizingMaskIntoConstraints = false;
NSLayoutConstraint(item: ad!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true;
NSLayoutConstraint(item: ad!, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 3).isActive = true;
}
}
}
func didFetchUserResponses(_ card: ACOAdaptiveCard, action: ACOBaseActionElement)
{
if(action.type == ACRActionType.openUrl){
let url = URL.init(string:action.url());
let svc = SFSafariViewController.init(url: url!);
self.present(svc, animated: true, completion: nil);
} else if action.type == ACRActionType.submit {
guard let userInputsAsJson = card.inputs() else { return }
guard let userInput = String(data: userInputsAsJson, encoding: .utf8) else { return }
let alert = UIAlertController(title: action.title(), message: "User Input: \(userInput) and Data: \(String(describing: action.data()))", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment