I have set up a UIApplicationShortcutItem that appears when I hard press on my app on the home screen. I have set up my ShortcutItem as static. My app is a soundboard that plays sounds when a button is pressed. To play the sounds I am using AVFoundation AVAudioPlayer to play the sounds. My question is, How do I play a sound when the shortcut item is pressed on the home screen?
This is the code in the AppDelegate I tried to use to play the sound when the shortcut item is pressed.
It is just a part of the entire AppDelegate code. I tried using some of Apple's example code.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
shortcutItemToProcess = shortcutItem
}
return true
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
// Alternatively, a shortcut item may be passed in through this delegate method if the app was
// still in memory when the Home screen quick action was used. Again, store it for processing.
shortcutItemToProcess = shortcutItem
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Is there a shortcut item that has not yet been processed?
if let shortcutItem = shortcutItemToProcess {
// In this sample an alert is being shown to indicate that the action has been triggered,
// but in real code the functionality for the quick action would be triggered
playSound(sound: "davie504-epic", type: "wav")
let alertController = UIAlertController(title: "Quick Action", message: "\(shortcutItem.type) triggered", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
window?.rootViewController?.present(alertController, animated: true, completion: nil)
// Reset the shortcut item so it's never processed twice.
shortcutItemToProcess = nil
}
}
Thank you for your time.