Prevents deleting legacy sideloaded apps

This commit is contained in:
Riley Testut
2020-01-13 11:22:40 -08:00
parent 7e2f2a5877
commit c2048f3814
2 changed files with 22 additions and 4 deletions

View File

@@ -20,6 +20,8 @@ extension UserDefaults
@NSManaged var isDebugModeEnabled: Bool @NSManaged var isDebugModeEnabled: Bool
@NSManaged var presentedLaunchReminderNotification: Bool @NSManaged var presentedLaunchReminderNotification: Bool
@NSManaged var legacySideloadedApps: [String]?
func registerDefaults() func registerDefaults()
{ {
self.register(defaults: [#keyPath(UserDefaults.isBackgroundRefreshEnabled): true]) self.register(defaults: [#keyPath(UserDefaults.isBackgroundRefreshEnabled): true])

View File

@@ -56,6 +56,18 @@ extension AppManager
do do
{ {
let installedApps = try context.fetch(fetchRequest) let installedApps = try context.fetch(fetchRequest)
if UserDefaults.standard.legacySideloadedApps == nil
{
// First time updating apps since updating AltStore to use custom UTIs,
// so cache all existing apps temporarily to prevent us from accidentally
// deleting them due to their custom UTI not existing (yet).
let apps = installedApps.map { $0.bundleIdentifier }
UserDefaults.standard.legacySideloadedApps = apps
}
let legacySideloadedApps = Set(UserDefaults.standard.legacySideloadedApps ?? [])
for app in installedApps for app in installedApps
{ {
let uti = UTTypeCopyDeclaration(app.installedAppUTI as CFString)?.takeRetainedValue() as NSDictionary? let uti = UTTypeCopyDeclaration(app.installedAppUTI as CFString)?.takeRetainedValue() as NSDictionary?
@@ -66,12 +78,10 @@ extension AppManager
} }
else else
{ {
if uti == nil && !UIApplication.shared.canOpenURL(app.openAppURL) if uti == nil && !legacySideloadedApps.contains(app.bundleIdentifier)
{ {
// This UTI is not declared by any apps, which means this app has been deleted by the user. // This UTI is not declared by any apps, which means this app has been deleted by the user.
// We also check canOpenURL as a fallback for apps installed before we switched to using custom UTIs. // This app is also not a legacy sideloaded app, so we can assume it's fine to delete it.
// canOpenURL always returns NO for apps not declared in our Info.plist,
// so it should not affect UTI-based installation checks.
context.delete(app) context.delete(app)
} }
} }
@@ -428,6 +438,12 @@ private extension AppManager
do { try installedApp.managedObjectContext?.save() } do { try installedApp.managedObjectContext?.save() }
catch { print("Error saving installed app.", error) } catch { print("Error saving installed app.", error) }
} }
if let index = UserDefaults.standard.legacySideloadedApps?.firstIndex(of: installedApp.bundleIdentifier)
{
// No longer a legacy sideloaded app, so remove it from cached list.
UserDefaults.standard.legacySideloadedApps?.remove(at: index)
}
} }
print("Finished operation!", context.bundleIdentifier) print("Finished operation!", context.bundleIdentifier)