Feedback

Super simple way to provide feedback with haptics and/or sound on iOS.

12
0
Swift

Feedback

The easiest way to provide feedback in the form of haptics and sounds for your users.

Providing feedback to your users is important; from indicating that a button was tapped, to indicating that an error occurred.

Overview

Feedback is an evolution of my other pod, HapticGenerator. Just like HapticGenerator,
Feedback provides an easy and coherent way to generate different types of haptic feedback.
Feedback goes a step further and allows you to also play sound as feedback, either with
or without accompanying haptics.

Feedback comes with a few default sounds, courtesy of Facebook’s Sound Kit, but also has
an option to allow you to use custom sounds, which means it will work with any brand.

Note that the provided sounds are for prototyping only (see Facebook’s terms for Sound Kit), in production you must provide your own custom sounds, which Feedback supports.

Installation

Feedback is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "FeedbackGenerator"

Usage

Using Feedback, just like HapticGenerator, is incredibly simple. You can create generators that
provide just haptics, haptics and sound, or just sound.

Here’s how to create a feedback generator that provides has a “success” haptic:

let successGenerator = Feedback(hapticType: .notification(.success))

Here’s how to create a feedback generator that just provides a default “error” sound:

let errorGenerator = Feedback(soundType: .notification(.error))

Here’s how to create a feedback generator that provides both a haptic and a sound:

let selectionGenerator = Feedback(hapticType: .selection, soundType: .selection)

Once you’ve created your generator, all you have to do to generate the feedback is this:

selectionGenerator.generateFeedback()

Optionally, if you know in advance when your feedback is likely to be generated, you
can prepare the system for its use:

selectionGenerator.prepareForUse()

You can also provide the name of a custom sound file to use with or without haptics:

let customSoundWithHaptic = Feedback(hapticType: .impact(.light), soundType: .custom(soundName: "customFileName", extension: "mp3"))

Requirements

Feedback works as far back as iOS 8, but haptics will only play on supported devices running iOS 10 or newer.
Under the hood, Feedback uses UIKit and AVFoundation.

Author

@kanecheshire

License

Feedback is available under the MIT license. See the LICENSE file for more info.