Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active September 29, 2018 19:43
Show Gist options
  • Save vialyx/5b055ab0ff2d19201b7f492368440008 to your computer and use it in GitHub Desktop.
Save vialyx/5b055ab0ff2d19201b7f492368440008 to your computer and use it in GitHub Desktop.
import IntentsUI
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// Call setup once at view did load
addObservers()
setupIntents()
}
private func setupIntents() {
// Use extension func to allocate new user activity using inner project Shortcusts enum
view.userActivity = NSUserActivity.with(.refreshList)
view.userActivity?.becomeCurrent()
// Code from official Apple Developer site
// API available check. That func available only for iOS 12 and later
if #available(iOS 12.0, *) {
// Get all shortcuts from OS center
INVoiceShortcutCenter.shared.getAllVoiceShortcuts { [weak self] (shortcuts, error) in
// Find existed shortcut with activityType is equal to our inner identifier
let refreshShortcut = shortcuts?.first(where: { (item) -> Bool in
return item.shortcut.userActivity?.activityType == Shortcuts.refreshList.identifier
})
guard let `self` = self, refreshShortcut == nil else { return }
DispatchQueue.main.async {
// Add Shortcut button to view. In my case that is UITableView's footer.
// You can provide just UIViewcontroller's view "self.view"
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50.0))
self.table.tableFooterView = view
self.addSiriButton(to: view)
}
}
} else {
// Fallback on earlier versions
}
}
// Code from official Apple Developer site
// Add an "Add to Siri" button to a view.
private func addSiriButton(to view: UIView) {
if #available(iOS 12.0, *) {
// Just add INUIAddVoiceShortcutButton to view
let button = INUIAddVoiceShortcutButton(style: .blackOutline)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
button.addTarget(self, action: #selector(addToSiri(_:)), for: .touchUpInside)
} else {
// Fallback on earlier versions
}
}
// Present the Add Shortcut view controller after the
// user taps the "Add to Siri" button.
@objc
func addToSiri(_ sender: Any) {
if #available(iOS 12.0, *) {
// Setup INShortcut with UserActivity using extension function that creates a new User Activity from inner project Shortcuts enum
let shortcut = INShortcut(userActivity: NSUserActivity.with(.refreshList))
let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
viewController.modalPresentationStyle = .formSheet
viewController.delegate = self // Object conforming to `INUIAddVoiceShortcutViewControllerDelegate`.
present(viewController, animated: true, completion: nil)
} else {
// Fallback on earlier versions
}
}
// Func that configure observers to handle info about Shortcusts notifications from AppDelegate
private func addObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: Shortcuts.refreshList.notification, object: nil)
}
@objc private func didBecomeActive() {
// Do you work. For example reload data (How it in my case)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment