【问题标题】:How can I mute incoming iPhone text messages programmatically?如何以编程方式静音传入的 iPhone 短信?
【发布时间】:2012-04-18 11:24:27
【问题描述】:

我目前正在尝试使用 AVSystemController 私有框架根据​​用户的选择来静音系统噪音。我目前正在通过拨打以下电话静音电话:[(AVSystemController object) setVolumeTo:0.0 forCategory:@"Ringtone"];

是否有命令对传入的短信执行此操作?我想这将基于该调用中确定的类别的变化。但是,我找不到要参考的类别列表。在我找到的 10 个 (Alert, Audio/Video, Ringtone, Voicemail, VoicemailGreeting, PhoneCall, TTYCall, RingtonePreview, Alarm, Record) 中,没有一个管理短信声音。有没有一个类别可以做到这一点?如果没有,有没有其他方法可以将传入文本的声音静音?

我知道这违反了 Apple 的无私有框架政策,但该应用不会在应用商店上架,所以没问题。我正在为最新版本的 IOS 使用最新版本的 Xcode 开发它,因此任何方法都可以实现。

【问题讨论】:

标签: iphone xcode sms system-sounds


【解决方案1】:

@Jessica,你不能这样做,因为它受到限制。如果您想在您的应用程序中试用它,那么您的应用程序可能会在 App Store 中被拒绝。

因此,使用公共 API 是不可能的。

您找到的链接使用的是私有 API,这些 API 未记录在案,也未保证按您期望的方式工作。如果您尝试发布调用私有 API 的 App Store 应用,它将被自动拒绝。

如果要检查是否静音,请使用以下代码,

    -(BOOL)silenced {
         #if TARGET_IPHONE_SIMULATOR
             // return NO in simulator. Code causes crashes for some reason.
             return NO;
         #endif

        CFStringRef state;
        UInt32 propertySize = sizeof(CFStringRef);
        AudioSessionInitialize(NULL, NULL, NULL, NULL);
        AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
        if(CFStringGetLength(state) > 0)
                return NO;
        else
                return YES;

        }


For completeness, building off this link from Dan Bon, I implement the following method to solve this problem in my apps. One thing to note is that the code checks for the iPhone simulator first - executing the below code will crash the simulator. Anyone know why?

-(BOOL)silenced {
     #if TARGET_IPHONE_SIMULATOR
         // return NO in simulator. Code causes crashes for some reason.
     return NO;
     #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if(CFStringGetLength(state) > 0)
        return NO;
    else
        return YES;

}

在视图控制器中声明此权限,您只需检查

if ([self silenced]) {
     NSLog(@"silenced");

else {
     NSLog(@"not silenced");
}

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 1970-01-01
    • 2011-04-15
    • 2010-09-05
    • 2010-09-22
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多