// // PBReloadHelper.m // ATVWebRemote // // Created by Kevin Bradley on 3/21/17. // Copyright © 2017 nito. All rights reserved. // #import #import "NSTask.h" #import //#import "rocketbootstrap.h" #import "AppSupport/CPDistributedMessagingCenter.h" @interface LSApplicationProxy : NSObject @property (nonatomic,readonly) NSURL * bundleURL; @property (nonatomic,readonly) NSString * bundleIdentifier; @property (nonatomic,readonly) NSString * bundleType; //@synthesize bundleType=_bundleType - In the implementation block @property (nonatomic,readonly) NSString * localizedShortName; @end @interface NSDistributedNotificationCenter : NSNotificationCenter + (id)defaultCenter; - (void)addObserver:(id)arg1 selector:(SEL)arg2 name:(id)arg3 object:(id)arg4; - (void)postNotificationName:(id)arg1 object:(id)arg2 userInfo:(id)arg3; @end @interface PBSMutableAppState : NSObject -(void)setEnabled:(BOOL)arg1 ; -(id)initWithApplicationIdentifer:(id)arg1 ; -(BOOL)isEnabled; @end @interface LSApplicationWorkspace : NSObject + (id) defaultWorkspace; - (void)_LSClearSchemaCaches; - (_Bool)_LSPrivateRebuildApplicationDatabasesForSystemApps:(_Bool)arg1 internal:(_Bool)arg2 user:(_Bool)arg3; -(id)allInstalledApplications; -(BOOL)_LSPrivateSyncWithMobileInstallation; -(BOOL)registerApplicationDictionary:(id)arg1 withObserverNotification:(int)arg2; -(BOOL)registerApplicationDictionary:(id)arg1 ; -(BOOL)registerApplication:(id)arg1 ; -(BOOL)unregisterApplication:(id)arg1 ; -(BOOL)registerPlugin:(id)arg1 ; -(BOOL)unregisterPlugin:(id)arg1 ; -(id)installedPlugins; @end @interface PBAppDepot : NSObject + (id)sharedInstance; @property(retain, nonatomic) NSMutableDictionary *internalAppState; - (id)_addAppStateForIdentifier:(id)arg1; - (void)_save; - (void)_setNeedsNotifyAppStateDidChange; - (void)_removeAppStateForIdentifier:(id)arg1; @end #import "PBReloadHelper.h" @implementation PBReloadHelper + (void)captureScreenshot { //CALayer *layer; // UIWindow *win = [[UIApplication sharedApplication] keyWindow]; //layer = win.layer; //UIGraphicsBeginImageContextWithOptions(win.bounds.size, NO, 1.0f); //[layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()]; //UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); //return screenImage; } -(void)runAppEnabler { //NSLog(@"#### for luck!"); [PBReloadHelper reloadApplications]; } /** On tvOS 10+ we need to manually remove the applications from PBAppDepot after moving / removing them from the /var/mobile/Applications folder. This method cycles filters allInstalledApplications in the application workspace to only applications with the bundleURL string containing /var/mobile/Applications/ loop through them and see which ones actually exist, if they dont, add the bundle ID to the deleted array that is returned. */ + (BOOL)containsNewApplication:(NSArray *)appIds { Class WSCLASS = objc_getClass("LSApplicationWorkspace"); id workspace = nil; if (WSCLASS != nil) { workspace = [WSCLASS defaultWorkspace]; } __block NSMutableArray *installedArray = [NSMutableArray new]; __block BOOL containsApplication = NO; NSArray *installedApplications = [workspace allInstalledApplications]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.bundleURL.absoluteString contains[cd] %@ or self.applicationIdentifier contains[cd] %@ ", @"/Applications/", @"com.nito.nitoTV4"]; NSArray *mobileApplications = [installedApplications filteredArrayUsingPredicate:pred]; //NSLog(@"new array: %@", newArray); NSFileManager *man = [NSFileManager defaultManager]; [mobileApplications enumerateObjectsUsingBlock:^(LSApplicationProxy * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSString *appPath = [[obj bundleURL] path]; //NSLog(@"thepath: %@", appPath); if ([man fileExistsAtPath:appPath]) { //NSLog(@"app removed: %@", [obj bundleIdentifier]); [installedArray addObject:[obj bundleIdentifier]]; } }]; [appIds enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (![installedArray containsObject:obj]) { *stop = YES; containsApplication = YES; } }]; return containsApplication; } + (NSArray *)deletedApplications { Class WSCLASS = objc_getClass("LSApplicationWorkspace"); id workspace = nil; if (WSCLASS != nil) { workspace = [WSCLASS defaultWorkspace]; } __block NSMutableArray *deletedArray = [NSMutableArray new]; NSArray *installedApplications = [workspace allInstalledApplications]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.bundleURL.absoluteString contains[cd] %@ or self.applicationIdentifier contains[cd] %@ ", @"/Applications/", @"com.nito.nitoTV4"]; NSArray *mobileApplications = [installedApplications filteredArrayUsingPredicate:pred]; //NSLog(@"new array: %@", newArray); NSFileManager *man = [NSFileManager defaultManager]; [mobileApplications enumerateObjectsUsingBlock:^(LSApplicationProxy * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSString *appPath = [[obj bundleURL] path]; //NSLog(@"thepath: %@", appPath); if (![man fileExistsAtPath:appPath]) { NSLog(@"app removed: %@", [obj bundleIdentifier]); [deletedArray addObject:[obj bundleIdentifier]]; } }]; return deletedArray; } + (void)newReloadApplications { NSFileManager *manager = [NSFileManager defaultManager]; NSString *whichKill = @"/tmp/usr/bin/killall"; if (![manager fileExistsAtPath:whichKill]) { whichKill = @"/usr/bin/killall"; } id appDepot = [objc_getClass("PBAppDepot") sharedInstance]; HBLogInfo(@"appDepot: %@", appDepot); NSMutableDictionary *installedAppStates = [appDepot internalAppState]; NSLog(@"installedAppStates: %@", installedAppStates); id nitoTV = [installedAppStates objectForKey:@"com.nito.nitoTV4"]; NSError *error = nil; NSString *whitelistFile = @"/var/mobile/Library/Preferences/com.nito.whitelist.plist"; NSString *recentlyDeleted = @"/var/mobile/Library/Preferences/com.nito.deleted.plist"; NSArray *whiteList = [NSArray arrayWithContentsOfFile:whitelistFile]; if (!whiteList) { whiteList = @[@"com.nito.nitoTV4"]; } NSMutableArray *recentDeletions = [NSMutableArray new]; NSMutableArray *mutableWhiteList = [whiteList mutableCopy]; BOOL addNito = [manager fileExistsAtPath:@"/Applications/nitoTV.app/nitoTV"]; NSMutableArray *appArray = [NSMutableArray new]; NSMutableArray *identifierArray = [NSMutableArray new]; BOOL addedApps = FALSE; NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]; for (NSString *app in apps) if ([app hasSuffix:@".app"]) { NSString *path = [@"/Applications" stringByAppendingPathComponent:app]; NSString *newPl = [path stringByAppendingPathComponent:@"Info.plist"]; NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:newPl]; NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]; NSLog(@"identifier: %@", identifier); id existingObject = (PBSMutableAppState*)[installedAppStates objectForKey:identifier]; if (existingObject != nil) { //[installedAppStates setObject:existingObject forKey:identifier]; if (![existingObject isEnabled]){ NSLog(@"its disabled: %@", existingObject); if (![mutableWhiteList containsObject:identifier]) { [mutableWhiteList addObject:identifier]; } NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path}; [appArray addObject:appDict]; [identifierArray addObject:identifier]; addedApps = YES; } } else { NSLog(@"didnt find object for key: %@", identifier); if (![mutableWhiteList containsObject:identifier]) { [mutableWhiteList addObject:identifier]; } NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path}; [appArray addObject:appDict]; [identifierArray addObject:identifier]; addedApps = YES; /* Class PBSMASC = objc_getClass("PBSMutableAppState"); id appState = [[PBSMASC alloc] initWithApplicationIdentifer:identifier]; [appState setEnabled:YES]; //[appState setCacheDeleting:YES]; [appState incrementCacheDeleting]; NSLog(@"app state: %@", appState); [installedAppStates setObject:appState forKey:identifier]; [workspace registerApplicationDictionary:info]; */ } } NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist"; [appArray writeToFile:pth atomically:YES]; Class WSCLASS = objc_getClass("LSApplicationWorkspace"); id workspace = nil; if (WSCLASS != nil) { workspace = [WSCLASS defaultWorkspace]; } NSArray *deletedApps = [self deletedApplications]; BOOL removedApps = ([deletedApps count] > 0); if ([workspace respondsToSelector:@selector(_LSPrivateSyncWithMobileInstallation)]){ if (addedApps || removedApps){ //rm /var/containers/Data/System/97D6E4BA-C0BF-408B-AF63-1836844381AE/Library/Caches/com.apple.LaunchServices-175-v2.csstore //delete the csstore file, its run through bash so the wildcards are completed properly //[NSTask launchedTaskWithLaunchPath:@"/bin/bash" arguments:@[@"/bin/rm", @"/var/containers/Data/System/*/Library/Caches/com.apple.LaunchServices*.csstore"]]; //FIXME: i dont like calling this chmod +x shell script //its probably a security risk of some type, but the line above //didnt work properly and i couldnt figure out why [NSTask launchedTaskWithLaunchPath:@"/bin/bash" arguments:@[@"/usr/bin/rmcache"]]; if (removedApps) { NSLog(@"app ids were deleted: %@", deletedApps); [deletedApps enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [mutableWhiteList removeObject:obj]; [recentDeletions addObject:obj]; NSLog(@"removing app state with id: %@", obj); [appDepot _removeAppStateForIdentifier:obj]; }]; } else { //no removed apps, need to make sure they don't mess with our reload anymore [[NSFileManager defaultManager] removeItemAtPath:recentlyDeleted error:nil]; } /* _LSPrivateSyncWithMobileInstallation will actually display the application and allow you to launch it, but the AppDepot (or something related to the installed app database) is potentially harshed or out of sync after we do this, which is why we always call the killall at the very bottom of this method, also if apps are deleted their state is not updated properly without a respring either */ if((addedApps || removedApps)){ NSLog(@"we can haz new app"); } } [workspace _LSClearSchemaCaches]; //may or may not be necessary [workspace _LSPrivateRebuildApplicationDatabasesForSystemApps:YES internal:YES user:YES]; [workspace _LSPrivateSyncWithMobileInstallation]; if ([workspace respondsToSelector:@selector(invalidateIconCache:)]){ NSLog(@"invalidate icon cache"); [workspace invalidateIconCache:nil]; } else { NSLog(@"## dont invalidate icon cache??"); } } [mutableWhiteList writeToFile:whitelistFile atomically:YES]; [recentDeletions writeToFile:recentlyDeleted atomically:YES]; if(addedApps){ NSLog(@"we can haz new app"); sleep(5); for (NSDictionary* o in appArray) { char retry=0; NSString* identifier = o[@"CFBundleIdentifier"]; retry_: NSLog(@"identifier: %@", identifier); id existingObject = [installedAppStates objectForKey:identifier]; if (existingObject != nil) { [existingObject setValue:[NSNumber numberWithBool:YES] forKey:@"_enabled"]; BOOL isEnabled = [existingObject isEnabled]; //[existingObject setEnabled:YES]; //[installedAppStates setObject: existingObject forKey: identifier]; } else { //[appDepot _addAppStateForIdentifier:identifier]; NSLog(@"didnt find object for key: %@, error!!!!!!", identifier); if(retry == 0) { NSLog(@"retry"); //NSLog(@"try to manually kick start substrate!"); //[NSTask launchedTaskWithLaunchPath:@"/usr/bin/nitoHelper" arguments:@[@"substrate", @"1", @"2"]]; //is9 = false; ///usr/bin/cynject 1 /Library/Frameworks/CydiaSubstrate.framework/Libraries/SubstrateLauncher.dylib retry = 1; goto retry_; } } } } //this notification is received by uicache binary so its knows that its safe to exit the runloop it creates so it can relay notifications back and forth during this convoluted process. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"kjc.AppEnabler.done" object:nil userInfo:nil]; //it beats killall -u mobile... right? //we dont have system() call anymore and posix_spawn is a pain to configure. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ //[@"science" writeToFile:@"/var/mobile/Library/Preferences/itshappening" atomically:YES encoding:NSUTF8StringEncoding error:nil]; [NSTask launchedTaskWithLaunchPath:whichKill arguments:@[@"-9", @"backboardd"]]; //[NSTask launchedTaskWithLaunchPath:whichKill arguments:@[@"-9", @"PineBoard", @"HeadBoard", @"lsd", @"installd"]]; }); } + (void)reloadApplications { [self newReloadApplications]; return; //at the moment in tvOS 10 morpheus extracts the bintools to /tmp rather than root, so .. for now, it could be in /tmp NSFileManager *manager = [NSFileManager defaultManager]; NSString *whichKill = @"/tmp/usr/bin/killall"; if (![manager fileExistsAtPath:whichKill]) { whichKill = @"/usr/bin/killall"; } /* PBAppDepot is where PineBoard keeps track of all the applications that are installed and whether or not they are new or enabled or whatever. PineBoardServices appears to communicate with the Depot to some degree over an XPC service that i couldn't quite figure out, a more /proper/ way to do this is probably through there. https://github.com/lechium/tvOS10Headers/blob/master/System/Library/PrivateFrameworks/PineBoardServices.framework/PBSAppDepotProxy.h the internalAppState is an NSDictionary where each key / value pair is the app id / PBMutableAppState that is related to each installed application. i.e. `{ ... "com.Moballo.tvbrowser" = "PBSMutableAppState(identifier=com.Moballo.tvbrowser, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)"; "com.apple.AdSheetPhone" = "PBSMutableAppState(identifier=com.apple.AdSheetPhone, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)"; "com.apple.CloudKit.ShareBear" = "PBSMutableAppState(identifier=com.apple.CloudKit.ShareBear, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)"; "com.apple.DiagnosticsService" = "PBSMutableAppState(identifier=com.apple.DiagnosticsService, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)"; ... } */ id appDepot = [objc_getClass("PBAppDepot") sharedInstance]; HBLogInfo(@"appDepot: %@", appDepot); NSMutableDictionary *installedAppStates = [appDepot internalAppState]; NSLog(@"installedAppStates: %@", installedAppStates); id nitoTV = [installedAppStates objectForKey:@"com.nito.nitoTV4"]; NSError *error = nil; NSString *whitelistFile = @"/var/mobile/Library/Preferences/com.nito.whitelist.plist"; NSString *recentlyDeleted = @"/var/mobile/Library/Preferences/com.nito.deleted.plist"; NSArray *whiteList = [NSArray arrayWithContentsOfFile:whitelistFile]; NSMutableArray *recentDeletions = [NSMutableArray new]; NSMutableArray *mutableWhiteList = [whiteList mutableCopy]; BOOL addNito = [manager fileExistsAtPath:@"/Applications/nitoTV.app/nitoTV"]; if (whiteList == nil){ if (addNito) { whiteList = @[@"com.nito.nitoTV4"]; mutableWhiteList = [whiteList mutableCopy]; } else { mutableWhiteList = [NSMutableArray new]; } } else { if (addNito) { NSLog(@"mutableWhiteList: %@", mutableWhiteList); if (![mutableWhiteList containsObject:@"com.nito.nitoTV4"]){ [mutableWhiteList addObject:@"com.nito.nitoTV4"]; } } } /* Loop through our /@"/var/mobile/Applications" folder and add them to a new plist file that will be injected inside the MobileInstallation framework when loading the list of applications that are installed. */ NSString *applicationPath = @"/var/mobile/Applications"; NSMutableArray *appArray = [NSMutableArray new]; NSMutableArray *identifierArray = [NSMutableArray new]; BOOL addedApps = FALSE; NSArray *apps = [manager contentsOfDirectoryAtPath:applicationPath error:&error]; NSArray *defaultApps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]; for (NSString *app in apps) { if ([app hasSuffix:@".app"]) { NSString *path = [applicationPath stringByAppendingPathComponent:app]; NSString *newPl = [path stringByAppendingPathComponent:@"Info.plist"]; NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:newPl]; //NSLog(@"info: %@", info); if (info != nil) { NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]; if(identifier != nil) { if (![mutableWhiteList containsObject:identifier]) { [mutableWhiteList addObject:identifier]; } NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path}; [appArray addObject:appDict]; [identifierArray addObject:identifier]; addedApps = YES; NSString *appPluginPath = [path stringByAppendingPathComponent:@"PlugIns"]; if ([manager fileExistsAtPath:appPluginPath]) { NSLog(@"found plugin folder for id: %@", identifier ); NSArray *plugins = [manager contentsOfDirectoryAtPath:appPluginPath error:&error]; [plugins enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([[obj pathExtension] isEqualToString:@"appex"]) { NSString *pluginPath = [appPluginPath stringByAppendingPathComponent:obj]; //NSLog(@"found appex: %@", pluginPath); NSString *pluginPl = [pluginPath stringByAppendingPathComponent:@"Info.plist"]; NSMutableDictionary *pluginInfo = [NSMutableDictionary dictionaryWithContentsOfFile:pluginPl]; //NSLog(@"pluginInfo: %@", pluginInfo); if (pluginInfo != nil) { NSString *pluginID = [pluginInfo objectForKey:@"CFBundleIdentifier"]; NSLog(@"id: %@", identifier); NSLog(@"pluginID: %@", pluginID); NSLog(@"pluginPath: %@", pluginPath); NSDictionary *pluginDict = @{@"ApplicationType": @"PluginKitPlugin", @"CFBundleIdentifier": pluginID, @"CodeInfoIdentifier": pluginID, @"Path": pluginPath, @"PluginOwnerBundleID": identifier}; //NSLog(@"pluginDict: %@", pluginDict); [appArray addObject:pluginDict]; } } }]; } } } } } if (nitoTV == nil && addNito) { NSLog(@"nitoTV has never been loaded before! force it to load"); NSString *path = @"/Applications/nitoTV.app"; NSString *newPl = [path stringByAppendingPathComponent:@"Info.plist"]; NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:newPl]; //NSLog(@"info: %@", info); if (info != nil) { NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]; if(identifier != nil) { if (![mutableWhiteList containsObject:identifier]) { [mutableWhiteList addObject:identifier]; } NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path}; [appArray addObject:appDict]; [identifierArray addObject:identifier]; addedApps = YES; NSString *appPluginPath = [path stringByAppendingPathComponent:@"PlugIns"]; if ([manager fileExistsAtPath:appPluginPath]) { NSLog(@"found plugin folder for id: %@", identifier ); NSArray *plugins = [manager contentsOfDirectoryAtPath:appPluginPath error:&error]; [plugins enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([[obj pathExtension] isEqualToString:@"appex"]) { NSString *pluginPath = [appPluginPath stringByAppendingPathComponent:obj]; //NSLog(@"found appex: %@", pluginPath); NSString *pluginPl = [pluginPath stringByAppendingPathComponent:@"Info.plist"]; NSMutableDictionary *pluginInfo = [NSMutableDictionary dictionaryWithContentsOfFile:pluginPl]; //NSLog(@"pluginInfo: %@", pluginInfo); if (pluginInfo != nil) { NSString *pluginID = [pluginInfo objectForKey:@"CFBundleIdentifier"]; NSDictionary *pluginDict = @{@"ApplicationType": @"PluginKitPlugin", @"CFBundleIdentifier": pluginID, @"CodeInfoIdentifier": pluginID, @"Path": pluginPath, @"PluginOwnerBundleID": identifier}; [appArray addObject:pluginDict]; } } }]; } } } } //the file that we store the installed applications in NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist"; [appArray writeToFile:pth atomically:YES]; Class WSCLASS = objc_getClass("LSApplicationWorkspace"); id workspace = nil; if (WSCLASS != nil) { workspace = [WSCLASS defaultWorkspace]; } int is9 = 0; //tvOS 10+ NSArray *deletedApps = [self deletedApplications]; BOOL removedApps = ([deletedApps count] > 0); if ([workspace respondsToSelector:@selector(_LSPrivateSyncWithMobileInstallation)]){ if (addedApps || removedApps){ //rm /var/containers/Data/System/97D6E4BA-C0BF-408B-AF63-1836844381AE/Library/Caches/com.apple.LaunchServices-175-v2.csstore //delete the csstore file, its run through bash so the wildcards are completed properly //[NSTask launchedTaskWithLaunchPath:@"/bin/bash" arguments:@[@"/bin/rm", @"/var/containers/Data/System/*/Library/Caches/com.apple.LaunchServices*.csstore"]]; //FIXME: i dont like calling this chmod +x shell script //its probably a security risk of some type, but the line above //didnt work properly and i couldnt figure out why [NSTask launchedTaskWithLaunchPath:@"/bin/bash" arguments:@[@"/usr/bin/rmcache"]]; if (removedApps) { NSLog(@"app ids were deleted: %@", deletedApps); [deletedApps enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [mutableWhiteList removeObject:obj]; [recentDeletions addObject:obj]; NSLog(@"removing app state with id: %@", obj); [appDepot _removeAppStateForIdentifier:obj]; }]; } else { //no removed apps, need to make sure they don't mess with our reload anymore [[NSFileManager defaultManager] removeItemAtPath:recentlyDeleted error:nil]; } /* _LSPrivateSyncWithMobileInstallation will actually display the application and allow you to launch it, but the AppDepot (or something related to the installed app database) is potentially harshed or out of sync after we do this, which is why we always call the killall at the very bottom of this method, also if apps are deleted their state is not updated properly without a respring either */ NSLog(@"before (addedApps || removedApps)"); if((addedApps || removedApps) && [self containsNewApplication:identifierArray]){ NSLog(@"we can haz new app"); [workspace _LSClearSchemaCaches]; //may or may not be necessary [workspace _LSPrivateRebuildApplicationDatabasesForSystemApps:YES internal:YES user:YES]; [workspace _LSPrivateSyncWithMobileInstallation]; } } } else { is9 = 1; //tvOS 9 /* lil bit of extra work for tvOS 9, we completely torch the lsd csstore file, from there we call a distributed notification that is relayed back to the uicache binary as far as i can remember we need to call back to uicache binary to trigger the workspace methods for recaching applications because of entitlement issues, our uicache binary that triggers these calls inside of Pineboard has the proper entitlements to make those calls, while Pineboard doesnt? */ //clear cache file NSString *file = @"/var/mobile/Library/Caches/com.apple.LaunchServices-135.csstore"; NSError *removeError = nil; [manager removeItemAtPath:file error:&removeError]; [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"kjc.AppEnabler.rebuild_appdb" object:nil userInfo:nil]; } [mutableWhiteList writeToFile:whitelistFile atomically:YES]; [recentDeletions writeToFile:recentlyDeleted atomically:YES]; /* this is a kludgy hack, we need to wait some time while our code is triggered in the AppEnabler tweak that injects into MobileInstallation. the code is triggered by _LSPrivateSyncWithMobileInstallation in tvOS 10+ and _LSPrivateRebuildApplicationDatabasesForSystemApps:* in tvOS 9 respectively. after that tweak does its magic our applications should be added (or removed) from PBAppDepot however, at that point the applications are added but still need to be enabled in the depot below we loop through every application in appArray (all apps in /var/mobile/Applications) and make sure all of their states are set to enabled in the PBAppDepot. At that point in tvOS 10+ we still need to 'respring' to get everything loaded and working without any issues. in tvOS 9 we don't need to respring, things should appear / disappear without any extra work. */ if(addedApps && [self containsNewApplication:identifierArray]){ NSLog(@"we can haz new app"); sleep(5); for (NSDictionary* o in appArray) { char retry=0; NSString* identifier = o[@"CFBundleIdentifier"]; retry_: NSLog(@"identifier: %@", identifier); id existingObject = [installedAppStates objectForKey:identifier]; if (existingObject != nil) { [existingObject setValue:[NSNumber numberWithBool:YES] forKey:@"_enabled"]; BOOL isEnabled = [existingObject isEnabled]; //[existingObject setEnabled:YES]; //[installedAppStates setObject: existingObject forKey: identifier]; } else { //[appDepot _addAppStateForIdentifier:identifier]; NSLog(@"didnt find object for key: %@, error!!!!!!", identifier); if(retry == 0) { NSLog(@"retry"); //NSLog(@"try to manually kick start substrate!"); //[NSTask launchedTaskWithLaunchPath:@"/usr/bin/nitoHelper" arguments:@[@"substrate", @"1", @"2"]]; //is9 = false; ///usr/bin/cynject 1 /Library/Frameworks/CydiaSubstrate.framework/Libraries/SubstrateLauncher.dylib retry = 1; goto retry_; } } } } //this notification is received by uicache binary so its knows that its safe to exit the runloop it creates so it can relay notifications back and forth during this convoluted process. [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"kjc.AppEnabler.done" object:nil userInfo:nil]; //it beats killall -u mobile... right? //we dont have system() call anymore and posix_spawn is a pain to configure. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ if (!is9){ //[@"science" writeToFile:@"/var/mobile/Library/Preferences/itshappening" atomically:YES encoding:NSUTF8StringEncoding error:nil]; [NSTask launchedTaskWithLaunchPath:whichKill arguments:@[@"-9", @"backboardd"]]; //[NSTask launchedTaskWithLaunchPath:whichKill arguments:@[@"-9", @"PineBoard", @"HeadBoard", @"lsd"]]; } }); } @end