3

In versions prior to iOS 8, there was a simple trick to hiding the system volume overlay. You'd simply create an MPVolumeView and embed it in your view hierarchy somewhere. This is documented here https://stackoverflow.com/a/7888977/3943258 and in many other Stack Overflow responses.

However, I'm finding this trick doesn't seem to work in iOS 8. I'm pulling my hair out trying to figure out how to fix it. Does anybody know if there's a way to do this in iOS 8?

One thing of note is that the app I'm doing this in has an active AVCaptureSession while I'm trying to hide the HUD (volume buttons act as the shutter on a camera). Not sure if there might be some side effects of that.

Community
  • 1
  • 1
Tim Johnsen
  • 1,471
  • 14
  • 33

3 Answers3

1

Alright, this abuses a private API but I've found that it works.

- (void)setVolumeHidden:(BOOL)hidden
{
    NSString *str1 = @"etSystemV";
    NSString *str2 = @"eHUDEnabled";
    NSString *selectorString = [NSString stringWithFormat:@"s%@olum%@:forAudioCategory:", str1, str2];
    SEL selector = NSSelectorFromString(selectorString);

    if ([[UIApplication sharedApplication] respondsToSelector:selector]) {
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIApplication instanceMethodSignatureForSelector:selector]];
        invocation.selector = selector;
        invocation.target = [UIApplication sharedApplication];
        BOOL value = !hidden;
        [invocation setArgument:&value atIndex:2];
        __unsafe_unretained NSString *category = @"Ringtone";
        [invocation setArgument:&category atIndex:3];
        [invocation invoke];
    }
}
Tim Johnsen
  • 1,471
  • 14
  • 33
1

Using MPVolumeView works in iOS 9.

let systemVolumeView = MPVolumeView(frame: CGRectMake(-500, -100, 0, 0))
view.addSubview(systemVolumeView)
sbook
  • 841
  • 1
  • 8
  • 13
  • I can't seem to get this to work unfortunately, is there any additional setup I might be missing? Thanks – trdavidson May 23 '16 at 19:45
  • @trdavidson are you saying that the system's volume HUD is still coming up? Are you sure the MPVolumeView is in an onscreen view hierarchy? – sbook May 24 '16 at 20:18
  • yes, I init the MPVolumeView and even constantly calla refresh method to make sure it is in the view hierarchy. Yet, the HUD is showing 50% of the time, which is odd. Any ideas? – trdavidson May 24 '16 at 21:14
  • The only thing I can think of is that my app has an active audio session which observes AVAudioSession's `outputVolume` property. Not sure that would affect it, but it could be worth a shot. – sbook May 24 '16 at 21:46
  • Gotcha, ok let me take a look there and I'll report back if I find a solution! – trdavidson May 24 '16 at 21:59
-1

Simplified solution a little bit:

#import <UAObfuscatedString/UAObfuscatedString.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL selector = NSSelectorFromString(NSMutableString.string.s.e.t.S.y.s.t.e.m.V.o.l.u.m.e.H.U.D.E.n.a.b.l.e.d.colon.f.o.r.A.u.d.i.o.C.a.t.e.g.o.r.y.colon);
if ([[UIApplication sharedApplication] respondsToSelector:selector]) {
    [[UIApplication sharedApplication] performSelector:selector withObject:@NO withObject:@"Ringtone"];
}
#pragma clang diagnostic pop
k06a
  • 17,755
  • 10
  • 70
  • 110