Он не работает в iOS 7, и он никогда не работал в iOS 6, если вы посмотрите, почему он не работает в iOS 7. Это решение основано на одном и том же коде, поэтому все они относятся к оригиналу автор.
- Держите
mute.caf
с вашего SharkfoodMuteSwitchDetector.
- Создайте новый класс под названием HASilentSwitchDetector (или что-то еще) или замените код в SharkfoodMuteSwitchDetector.
В заголовочном файле:
#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];
}
}
}];
Вы подключить этот код через IBAction в XIB или Раскадровка? – karthika
@ karthika: - Нет, над кодом написано в видеDidLoad, а затем в режиме действия кнопки silentSwitch включен или выключен. – Bhrigesh
проверить, подключено ли действие кнопки TouchUpInside или ValueChanged? Я также сталкиваюсь с этой проблемой в IOS7. – karthika