在iOS 7问题中检测静音开关

时间:2013-09-23 04:42:34

标签: ios xcode ios6 ios7

我使用以下代码检查iPhone静音开关是ON还是OFF: -

if (self)
   {
    self.detector = [SharkfoodMuteSwitchDetector shared];
    CheckInViewController* sself = self;
    self.detector.silentNotify = ^(BOOL silent)
       {
        [sself.silentSwitch setOn:silent animated:YES];
      };
   }

它在iOS 6及更低版本中运行良好,但在iOS 7中它总是给出TRUE值。所以,请任何人告诉,如何解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:3)

它在iOS 7中不起作用,如果你看看为什么它在iOS 7中不起作用,它在iOS 6中从未真正起作用。这个解决方案基于相同的代码,所以归功于原作者虽然。

  1. 从SharkfoodMuteSwitchDetector保留mute.caf
  2. 创建一个名为HASilentSwitchDetector(或其他)的新类,或替换SharkfoodMuteSwitchDetector中的代码。
  3. 在头文件中:

    #import <AudioToolbox/AudioToolbox.h>
    
    typedef void(^HASilentSwitchDetectorBlock)(BOOL success, BOOL silent);
    
    @interface HASilentSwitchDetector : NSObject
    
    + (void)ifMute:(HASilentSwitchDetectorBlock)then;
    
    @end
    

    在实施文件中:

    #import "HASilentSwitchDetector.h"
    
    void MuteSoundPlaybackComplete(SystemSoundID ssID, void *clientData)
    {
        //Completion
        NSDictionary *soundCompletion = CFBridgingRelease(clientData);
    
        //Mute
        NSTimeInterval interval = [soundCompletion[@"interval"] doubleValue];
        NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - interval;
        BOOL isMute = elapsed < 0.2; // mute.caf is .2s long...
    
        //Then
        HASilentSwitchDetectorBlock then = soundCompletion[@"then"];
        then(YES, isMute);
    
        //Cleanup
        SystemSoundID soundID = [soundCompletion[@"soundID"] integerValue];
        AudioServicesRemoveSystemSoundCompletion(soundID);
        AudioServicesDisposeSystemSoundID(soundID);
    }
    
    @implementation HASilentSwitchDetector
    
    + (void)ifMute:(HASilentSwitchDetectorBlock)then
    {
        //Check
        if ( !then ) {
            return;
        }
    
        //Create
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"mute" withExtension:@"caf"];
        SystemSoundID soundID;
        if ( AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID) == kAudioServicesNoError ) {
            //UI Sound
            UInt32 yes = 1;
            AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(soundID), &soundID,sizeof(yes), &yes);
    
            //Callback
            NSDictionary *soundCompletion = @{@"then" : [then copy], @"soundID" : @(soundID), @"interval" : @([NSDate timeIntervalSinceReferenceDate])};
            AudioServicesAddSystemSoundCompletion(soundID, CFRunLoopGetMain(), kCFRunLoopDefaultMode, MuteSoundPlaybackComplete, (void *)CFBridgingRetain(soundCompletion));
    
            //Play
            AudioServicesPlaySystemSound(soundID);
        } else {
            //Fail
            then(NO, NO);
        }
    }
    
    @end
    

    像这样使用:

    [HASilentSwitchDetector ifMute:^(BOOL success, BOOL silent) {
        if ( success ) {
            if ( ![[NSUserDefaults standardUserDefaults] boolForKey:forKey:kHasShownMuteWarning] && silent ) {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kHasShownMuteWarning];
                [[[UIAlertView alloc] initWithTitle:[@"Mute Warning" localized] message:[NSString stringWithFormat:[@"This %@'s mute switch is on.  To ensure your alarm will be audible, unmute your device." localized], [[[UIDevice currentDevice] isiPad]? @"iPad" : @"iPhone" localized]] delegate:nil cancelButtonTitle:nil otherButtonTitles:[@"Ok" localized], nil] show];
            }
        }
    }];