Skip to content

Instantly share code, notes, and snippets.

@syropian
Created August 22, 2022 23:13
Show Gist options
  • Save syropian/e1f2f32eed796ccbae33f5a829225831 to your computer and use it in GitHub Desktop.
Save syropian/e1f2f32eed796ccbae33f5a829225831 to your computer and use it in GitHub Desktop.
An Expo config plugin to automatically hide the home indicator on iOS
const { withAppDelegate } = require('@expo/config-plugins')
function withHiddenHomeIndicator(expoConfig) {
return withAppDelegate(expoConfig, (config) => {
const { modResults } = config
const { contents } = modResults
const lines = contents.split('\n')
const importIndex = lines.findIndex((line) => /^#import "AppDelegate.h"/.test(line))
modResults.contents = [
'#import <objc/runtime.h>',
...lines.slice(0, importIndex + 1),
...lines.slice(importIndex + 1),
'@implementation UIViewController (Swizzling)',
'+ (void)load',
'{',
' static dispatch_once_t onceToken;',
' dispatch_once(&onceToken, ^{',
' Class classVC = [self class];',
' SEL originalSelector = @selector(prefersHomeIndicatorAutoHidden);',
' SEL swizzledSelector = @selector(swizzledPrefersHomeIndicatorAutoHidden);',
' Method originalMethod = class_getInstanceMethod(classVC, originalSelector);',
' Method swizzledMethod = class_getInstanceMethod(classVC, swizzledSelector);',
' const BOOL didAdd = class_addMethod(classVC, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));',
' if (didAdd)',
' class_replaceMethod(classVC, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));',
' else',
' method_exchangeImplementations(originalMethod, swizzledMethod);',
' });',
'}',
'- (BOOL)prefersHomeIndicatorAutoHidden {',
' return NO;',
'}',
'- (BOOL)swizzledPrefersHomeIndicatorAutoHidden {',
' return YES;',
'}',
'@end',
].join('\n')
return config
})
}
module.exports = withHiddenHomeIndicator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment