Hide HUD volume in iOS 8

Before iOS 8, there was a simple trick to hide the system volume overlay. You just create MPVolumeView

and insert it into your view hierarchy somewhere. This is documented here on StackOverflow question and answer ... and many other answers.

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

It should be noted that the app I am doing this in has an active AVCaptureSession

one while I try to hide the HUD (the volume buttons act like a shutter on the camera). Not sure if there might be some side effects.


source to share

3 answers

Ok, this breaks the private API, but I found 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];




Usage MPVolumeView

works in iOS 9.

let systemVolumeView = MPVolumeView(frame: CGRectMake(-500, -100, 0, 0))




Simplified solution:

#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




All Articles