7

I have been working with Apple Watch OS 2 for a while, but have not succeeded in triggering vibration on Apple Watch standalone app in Swift, using Xcode Version 7.0 beta 6 (7A192o).

All tutorials using vibration on iPhone in Swift look like:

 import AudioToolbox
 ...
 AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

But I cannot import the AudioToolbox library.

I get the error:

Could not build Objective-C module "AudioToolbox"

Has anyone already found the way to do it?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jiri
  • 89
  • 1
  • 1
  • 2

6 Answers6

14

You can generate haptic & auditory feedback, using:

WKInterfaceDevice.currentDevice().playHaptic(.Click)

'Click' is one of the 'cases' (types of vibration/beeps) that you can choose, cfr:

enum WKHapticType : Int {
 

case Notification
 
case DirectionUp

case DirectionDown
 
case Success

case Failure
 
case Retry
 
case Start
 
case Stop
 
case Click

}

This only works on the device, not in the simulator.

LucPts
  • 141
  • 5
7

In Swift 3:

WKInterfaceDevice.current().play(.success)
Zoltan
  • 356
  • 3
  • 14
4

I'm not a expert in WatchKit but I know that with Watch OS 2 you can use:

WKInterfaceDevice().playHaptic(.Notification)

The function playHaptic called on a WKInterfaceDevice instance engage the haptic engine in Apple Watch.

See more in the Apple documentation.

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
2

The Correct format is

WKInterfaceDevice.currentDevice().playHaptic(.Notification)

You can easily play the notification sound on watch

Manish Gumbal
  • 283
  • 3
  • 13
1

Function :

WKInterfaceDevice().playHaptic(.Notification)

Here you have : Documentation

Onvil
  • 21
  • 2
0
WKInterfaceDevice.current().play(.failure) 

in Swift 3

Dylan Murphy
  • 167
  • 1
  • 13
  • As an inexperienced programmer myself I would much prefer an answer that gets straight to the point rather than a 500-word answer explaining functions and other stuff that the inexperienced programmer has no idea about. There this is my answer and is better than others because I get to the point. – Dylan Murphy Jun 22 '17 at 14:39