This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize in viewDidLoad | |
let mediumImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .medium) | |
// Prepare in viewDidAppear | |
mediumImpactFeedbackGenerator.prepare() | |
// Play haptic signal | |
if let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int { | |
switch feedbackSupportLevel { | |
case 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize in viewDidLoad | |
var mediumImpactFeedbackGenerator: UIImpactFeedbackGenerator? = nil | |
if UIDevice.current.hasHapticFeedback { | |
mediumImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .medium) | |
} | |
// Prepare in viewDidAppear | |
mediumImpactFeedbackGenerator?.prepare() | |
// Play the desired haptic signal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIDevice { | |
enum DevicePlatform: String { | |
case other = "Old Device" | |
case iPhone6S = "iPhone 6S" | |
case iPhone6SPlus = "iPhone 6S Plus" | |
case iPhone7 = "iPhone 7" | |
case iPhone7Plus = "iPhone 7 Plus" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let selectionFeedbackGenerator = UISelectionFeedbackGenerator() | |
// Prepare shortly before playing | |
selectionFeedbackGenerator.prepare() | |
// Play the haptic signal | |
selectionFeedbackGenerator.selectionChanged() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let impactFeedbackGenerator: ( | |
light: UIImpactFeedbackGenerator, | |
medium: UIImpactFeedbackGenerator, | |
heavy: UIImpactFeedbackGenerator) = ( | |
UIImpactFeedbackGenerator(style: .light), | |
UIImpactFeedbackGenerator(style: .medium), | |
UIImpactFeedbackGenerator(style: .heavy) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let lightImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light) | |
// Prepare shortly before playing | |
lightImpactFeedbackGenerator.prepare() | |
// Play the haptic signal | |
lightImpactFeedbackGenerator.impactOccurred() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let notificationFeedbackGenerator = UINotificationFeedbackGenerator() | |
// We can inform the feedback generator that it will likely receive events soon, | |
// which basically 'wakes up' the device's taptic engine for a few seconds, | |
// preparing it for use without any latency. You can call this function as many | |
// times as you want. Good times to call this are when a gesture begins and right | |
// after a haptic signal is played. | |
notificationFeedbackGenerator.prepare() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AudioToolbox.AudioServices | |
// 'Peek' feedback (weak boom) | |
let peek = SystemSoundID(1519) | |
AudioServicesPlaySystemSound(peek) | |
// 'Pop' feedback (strong boom) | |
let pop = SystemSoundID(1520) | |
AudioServicesPlaySystemSound(pop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AudioToolbox.AudioServices | |
// kSystemSoundID_Vibrate is just a UInt32 with a value of 4095 | |
let vibrate = SystemSoundID(kSystemSoundID_Vibrate) | |
AudioServicesPlaySystemSound(vibrate) | |
// ... or if you want a completion block to be called after the vibration finishes | |
let vibrate = SystemSoundID(kSystemSoundID_Vibrate) // 4095 | |
AudioServicesPlaySystemSoundWithCompletion(vibrate, { | |
print("did vibrate") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Disk | |
do { | |
try Disk.save(posts, to: .documents, as: "posts.json") | |
let retrieved = try Disk.retrieve("posts.json", from: .documents, as: [Post].self) | |
} catch { | |
// ... | |
} |