0

I´m testing the keyboard extension with new language swift on iOS, but I can´t play a sound for keyPressed event, and there is a delay, about 10 seconds when I click the key.

This is my code:

@IBAction func keyPressed(button: UIButton) {
    var string = button.titleLabel!.text
    (textDocumentProxy as UIKeyInput).insertText("\(string!)")
    AudioServicesDisposeSystemSoundID(1104)
    AudioServicesPlaySystemSound(1104)
    UIView.animateWithDuration(0.2, animations: {
        button.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0)
        }, completion: {(_) -> Void in
            button.transform =
            CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
    })
}

Thanks in advance for any comment or suggestion...

jezuz
  • 413
  • 2
  • 5
  • 13
  • Why are you disposing of the system sound every time the key is pressed? This seems highly inefficient.. – lxt Nov 02 '14 at 04:53
  • I´ve tried the disposed at the didLoad but I´m getting the same results. – jezuz Nov 02 '14 at 04:56
  • I´m based on this code: https://www.dropbox.com/s/gfewd0qekxr5u4i/CustomKeyboardFinal.zip?dl=0 – jezuz Nov 02 '14 at 05:18

2 Answers2

0

You need to play the sound in another thread. The user needs to allow full access for this to work. So make sure you request for this in the info.plist of the extension

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),     
{
    AudioServicesPlaySystemSound(1104);
});    

For Objective-C just add the ^ sign in front of the {

ChrisBorg
  • 1,418
  • 17
  • 27
0

You need to move AudioServicesDisposeSystemSoundID(1104) to deinit method.

Shmidt
  • 16,436
  • 18
  • 88
  • 136