在iOS键盘扩展中播放系统点击声音

时间:2014-09-27 13:56:15

标签: ios audio ios-keyboard-extension

我已按照这两个链接上的说明进行操作:

How to play keyboard click sound in custom keyboard?

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html

并完成以下操作:

标题:

@interface Key : UIView <UIInputViewAudioFeedback>

实现:

- (BOOL) enableInputClicksWhenVisible {
    return YES;
}

- (void)tapped:(UITapGestureRecognizer *)sender
{
    [[UIDevice currentDevice] playInputClick];
    [self.delegate keyHit:_title];
}

然而它仍然无效。我错过了什么?

2 个答案:

答案 0 :(得分:1)

只要您想在键盘扩展程序中播放系统点击声音,请尝试使用此代码:

+ (void)keyboardClickSound {

    // Check system preference for current setting of Keyboard Click Sound.
    CFStringRef applicationID = CFSTR("/var/mobile/Library/Preferences/com.apple.preferences.sounds");
    Boolean keyExistsAndHasValidFormat;
    BOOL enableInputClicks
    = CFPreferencesGetAppBooleanValue(CFSTR("keyboard"), applicationID, &keyExistsAndHasValidFormat);

    // Note: If the key does not exist because it has never been changed by a user,
    //       set it to the default, YES.
    if (!keyExistsAndHasValidFormat)
        enableInputClicks = YES;

    // Play Keyboard Click Sound, if enabled.
    // Note: If Open Access=Off, no click sound.
    if (enableInputClicks)
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                   ^{ AudioServicesPlaySystemSound(1104); });
}

答案 1 :(得分:1)

如果您的键盘扩展应用包含&#34; RequestsOpenAccess&#34;字段为YES,
然后键盘点击声音必须&#34;允许完全访问&#34;从中切换到开 一般&gt;键盘&gt;键盘&gt; Your_keyboard设置。

如果已关闭,则无法播放键盘按键音。