Skip to content

Instantly share code, notes, and snippets.

@russell-archer
Created October 8, 2018 23:56
Show Gist options
  • Save russell-archer/d055838a6bcb58712f413d48058465ea to your computer and use it in GitHub Desktop.
Save russell-archer/d055838a6bcb58712f413d48058465ea to your computer and use it in GitHub Desktop.
ShowRandomColor. Implementing the app extension UI
//
// IntentViewController.swift
// ColorIntentUI
//
// Created by Russell Archer on 05/10/2018.
// Copyright © 2018 Russell Archer. All rights reserved.
//
import IntentsUI
import ColorGenerator
class IntentViewController: UIViewController, INUIHostedViewControlling {
@IBOutlet weak var colorNameLabel: UILabel!
@IBOutlet weak var colorView: UIView!
var desiredSize: CGSize { return self.extensionContext!.hostedViewMaximumAllowedSize }
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - INUIHostedViewControlling
// Present to UI for the shortcut
func configureView(for parameters: Set<INParameter>,
of interaction: INInteraction,
interactiveBehavior: INUIInteractiveBehavior,
context: INUIHostedViewContext,
completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {
// Is this the intent we expected?
guard interaction.intent is ShowRandomColorIntent else {
completion(false, Set(), .zero)
return
}
// Load the random color that was saved in the intent handler from our shared user defaults plist
let defaults = UserDefaults.init(suiteName: "group.com.rarcher.ShowRandomColor.Shared")
let name = defaults!.string(forKey: "colorName")
// Recreate the color
let rgbInfo = Generate.rgb(colorName: name!)
let color = UIColor(red: rgbInfo.r, green: rgbInfo.g, blue: rgbInfo.b, alpha: 1)
// Set the app extension UI view color and name
colorView.backgroundColor = color
colorNameLabel.text = name
// Tell iOS all went well
completion(true, parameters, desiredSize)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment