检测iOS 7中的无声开关问题

我正在使用以下代码来检查iPhone无声开关是打开还是closures: –

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值。 所以,请任何人告诉,如何解决这个问题。

提前致谢。

它在iOS 7中不起作用,如果你看看它为什么在iOS 7中不起作用,它从来没有在iOS 6真正的工作。这个解决scheme是基于相同的代码,所以相信原来的作者。

  1. 保持你的SharkfoodMuteSwitchDetector mute.caf。
  2. 创build一个名为HASilentSwitchDetector(或其他)的新类,或者replaceSharkfoodMuteSwitchDetector中的代码。

在头文件中:

 #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]; } } }];