简体   繁体   中英

iOS swift custom keyboard no sound and delay

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...

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 {

您需要将AudioServicesDisposeSystemSoundID(1104)移动到deinit方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM