Files
SideStore/AltStoreCore/Model/InstalledApp.swift

452 lines
18 KiB
Swift
Raw Normal View History

//
// InstalledApp.swift
// AltStore
//
// Created by Riley Testut on 5/20/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import Foundation
import CoreData
import AltSign
2023-01-19 07:52:47 -08:00
import SemanticVersion
extension InstalledApp
{
public static var freeAccountActiveAppsLimit: Int {
2024-12-07 17:45:09 +05:30
if UserDefaults.standard.isAppLimitDisabled
{
// MacDirtyCow exploit allows users to remove 3-app limit, so return 10 to match App ID limit per-week.
// Don't return nil because that implies there is no limit, which isn't quite true due to App ID limit.
return 10
}
else
{
// Free developer accounts are limited to only 3 active sideloaded apps at a time as of iOS 13.3.1.
return 3
}
}
}
public protocol InstalledAppProtocol: Fetchable
2020-01-21 16:53:34 -08:00
{
var name: String { get }
var bundleIdentifier: String { get }
var resignedBundleIdentifier: String { get }
var version: String { get }
var refreshedDate: Date { get }
var expirationDate: Date { get }
var installedDate: Date { get }
}
@objc(InstalledApp)
public class InstalledApp: NSManagedObject, InstalledAppProtocol
{
/* Properties */
@NSManaged public var name: String
@NSManaged public var bundleIdentifier: String
@NSManaged public var resignedBundleIdentifier: String
@NSManaged public var version: String
@NSManaged public var buildVersion: String
@NSManaged public var refreshedDate: Date
@NSManaged public var expirationDate: Date
@NSManaged public var installedDate: Date
@NSManaged public var isActive: Bool
@NSManaged public var needsResign: Bool
@NSManaged public var hasAlternateIcon: Bool
@NSManaged public var certificateSerialNumber: String?
@NSManaged public var storeBuildVersion: String?
/* Transient */
@NSManaged public var isRefreshing: Bool
/* Relationships */
@NSManaged public var storeApp: StoreApp?
@NSManaged public var team: Team?
@NSManaged public var appExtensions: Set<InstalledExtension>
@NSManaged public private(set) var loggedErrors: NSSet /* Set<LoggedError> */ // Use NSSet to avoid eagerly fetching values.
public var isSideloaded: Bool {
2019-07-28 15:51:36 -07:00
return self.storeApp == nil
}
2023-01-19 07:52:47 -08:00
@objc public var hasUpdate: Bool {
if self.storeApp == nil { return false }
merge AltStore 1.6.3, add dynamic anisette lists, merge SideJITServer integration * Change error from Swift.Error to NSError * Adds ResultOperation.localizedFailure * Finish Riley's monster commit 3b38d725d7e8e45fb2c0cb465a3968828616c209 May the Gods have mercy on my soul. * Fix format strings I broke * Include "Enable JIT" errors in Error Log * Fix minimuxer status checking * [skip ci] Update the no wifi message to include VPN * Opens Error Log when tapping ToastView * Fixes Error Log context menu covering cell content * Fixes Error Log context menu appearing while scrolling * Fixes incorrect Search FAQ URL * Fix Error Log showing UIAlertController on iOS 14+ * Fix Error Log not showing UIAlertController on iOS <=13 * Fix wrong color in AuthenticationViewController * Fix typo * Fixes logging non-AltServerErrors as AltServerError.underlyingError * Limits quitting other AltStore/SideStore processes to database migrations * Skips logging cancelled errors * Replaces StoreApp.latestVersion with latestSupportedVersion + latestAvailableVersion We now store the latest supported version as a relationship on StoreApp, rather than the latest available version. This allows us to reference the latest supported version in predicates and sort descriptors. However, we kept the underlying Core Data property name the same to avoid extra migration. * Conforms OperatingSystemVersion to Comparable * Parses AppVersion.minOSVersion/maxOSVersion from source JSON * Supports non-NSManagedObjects for @Managed properties This allows us to use @Managed with properties that may or may not be NSManagedObjects at runtime (e.g. protocols). If they are, Managed will keep strong reference to context like before. * Supports optional @Managed properties * Conforms AppVersion to AppProtocol * Verifies min/max OS version before downloading app + asks user to download older app version if necessary * Improves error message when file does not exist at AppVersion.downloadURL * Removes unnecessary StoreApp convenience properties * Removes unnecessary StoreApp convenience properties as well as fix other issues * Remove Settings bundle, add SwiftUI view instead Fix refresh all shortcut intent * Update AuthenticationOperation.swift Signed-off-by: June Park <rjp2030@outlook.com> * Fix build issues given by develop * Add availability check to fix CI build(?) * If it's gonna be that way... --------- Signed-off-by: June Park <rjp2030@outlook.com> Co-authored-by: nythepegasus <nythepegasus84@gmail.com> Co-authored-by: Riley Testut <riley@rileytestut.com> Co-authored-by: ny <me@nythepegas.us>
2024-08-06 10:43:52 +09:00
if self.storeApp!.latestSupportedVersion == nil { return false }
2023-01-19 07:52:47 -08:00
let currentVersion = SemanticVersion(self.version)
merge AltStore 1.6.3, add dynamic anisette lists, merge SideJITServer integration * Change error from Swift.Error to NSError * Adds ResultOperation.localizedFailure * Finish Riley's monster commit 3b38d725d7e8e45fb2c0cb465a3968828616c209 May the Gods have mercy on my soul. * Fix format strings I broke * Include "Enable JIT" errors in Error Log * Fix minimuxer status checking * [skip ci] Update the no wifi message to include VPN * Opens Error Log when tapping ToastView * Fixes Error Log context menu covering cell content * Fixes Error Log context menu appearing while scrolling * Fixes incorrect Search FAQ URL * Fix Error Log showing UIAlertController on iOS 14+ * Fix Error Log not showing UIAlertController on iOS <=13 * Fix wrong color in AuthenticationViewController * Fix typo * Fixes logging non-AltServerErrors as AltServerError.underlyingError * Limits quitting other AltStore/SideStore processes to database migrations * Skips logging cancelled errors * Replaces StoreApp.latestVersion with latestSupportedVersion + latestAvailableVersion We now store the latest supported version as a relationship on StoreApp, rather than the latest available version. This allows us to reference the latest supported version in predicates and sort descriptors. However, we kept the underlying Core Data property name the same to avoid extra migration. * Conforms OperatingSystemVersion to Comparable * Parses AppVersion.minOSVersion/maxOSVersion from source JSON * Supports non-NSManagedObjects for @Managed properties This allows us to use @Managed with properties that may or may not be NSManagedObjects at runtime (e.g. protocols). If they are, Managed will keep strong reference to context like before. * Supports optional @Managed properties * Conforms AppVersion to AppProtocol * Verifies min/max OS version before downloading app + asks user to download older app version if necessary * Improves error message when file does not exist at AppVersion.downloadURL * Removes unnecessary StoreApp convenience properties * Removes unnecessary StoreApp convenience properties as well as fix other issues * Remove Settings bundle, add SwiftUI view instead Fix refresh all shortcut intent * Update AuthenticationOperation.swift Signed-off-by: June Park <rjp2030@outlook.com> * Fix build issues given by develop * Add availability check to fix CI build(?) * If it's gonna be that way... --------- Signed-off-by: June Park <rjp2030@outlook.com> Co-authored-by: nythepegasus <nythepegasus84@gmail.com> Co-authored-by: Riley Testut <riley@rileytestut.com> Co-authored-by: ny <me@nythepegas.us>
2024-08-06 10:43:52 +09:00
let latestVersion = SemanticVersion(self.storeApp!.latestSupportedVersion!.version)
2023-01-19 07:52:47 -08:00
if currentVersion == nil || latestVersion == nil {
// One of the versions is not valid SemVer, fall back to comparing the version strings by character
merge AltStore 1.6.3, add dynamic anisette lists, merge SideJITServer integration * Change error from Swift.Error to NSError * Adds ResultOperation.localizedFailure * Finish Riley's monster commit 3b38d725d7e8e45fb2c0cb465a3968828616c209 May the Gods have mercy on my soul. * Fix format strings I broke * Include "Enable JIT" errors in Error Log * Fix minimuxer status checking * [skip ci] Update the no wifi message to include VPN * Opens Error Log when tapping ToastView * Fixes Error Log context menu covering cell content * Fixes Error Log context menu appearing while scrolling * Fixes incorrect Search FAQ URL * Fix Error Log showing UIAlertController on iOS 14+ * Fix Error Log not showing UIAlertController on iOS <=13 * Fix wrong color in AuthenticationViewController * Fix typo * Fixes logging non-AltServerErrors as AltServerError.underlyingError * Limits quitting other AltStore/SideStore processes to database migrations * Skips logging cancelled errors * Replaces StoreApp.latestVersion with latestSupportedVersion + latestAvailableVersion We now store the latest supported version as a relationship on StoreApp, rather than the latest available version. This allows us to reference the latest supported version in predicates and sort descriptors. However, we kept the underlying Core Data property name the same to avoid extra migration. * Conforms OperatingSystemVersion to Comparable * Parses AppVersion.minOSVersion/maxOSVersion from source JSON * Supports non-NSManagedObjects for @Managed properties This allows us to use @Managed with properties that may or may not be NSManagedObjects at runtime (e.g. protocols). If they are, Managed will keep strong reference to context like before. * Supports optional @Managed properties * Conforms AppVersion to AppProtocol * Verifies min/max OS version before downloading app + asks user to download older app version if necessary * Improves error message when file does not exist at AppVersion.downloadURL * Removes unnecessary StoreApp convenience properties * Removes unnecessary StoreApp convenience properties as well as fix other issues * Remove Settings bundle, add SwiftUI view instead Fix refresh all shortcut intent * Update AuthenticationOperation.swift Signed-off-by: June Park <rjp2030@outlook.com> * Fix build issues given by develop * Add availability check to fix CI build(?) * If it's gonna be that way... --------- Signed-off-by: June Park <rjp2030@outlook.com> Co-authored-by: nythepegasus <nythepegasus84@gmail.com> Co-authored-by: Riley Testut <riley@rileytestut.com> Co-authored-by: ny <me@nythepegas.us>
2024-08-06 10:43:52 +09:00
return self.version < self.storeApp!.latestSupportedVersion!.version
2023-01-19 07:52:47 -08:00
}
return currentVersion! < latestVersion!
}
public var appIDCount: Int {
return 1 + self.appExtensions.count
}
public var requiredActiveSlots: Int {
let requiredActiveSlots = UserDefaults.standard.activeAppLimitIncludesExtensions ? self.appIDCount : 1
return requiredActiveSlots
}
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
{
super.init(entity: entity, insertInto: context)
}
public init(resignedApp: ALTApplication, originalBundleIdentifier: String, certificateSerialNumber: String?, storeBuildVersion: String?, context: NSManagedObjectContext)
{
super.init(entity: InstalledApp.entity(), insertInto: context)
self.bundleIdentifier = originalBundleIdentifier
print("InstalledApp `self.bundleIdentifier`: \(self.bundleIdentifier)")
self.refreshedDate = Date()
self.installedDate = Date()
self.expirationDate = self.refreshedDate.addingTimeInterval(60 * 60 * 24 * 7) // Rough estimate until we get real values from provisioning profile.
// In practice this update() is redundant because we always call update() again after init from callers,
// but better to have an init that is guaranteed to successfully initialize an object
// than one that has a hidden assumption a second method will be called.
self.update(resignedApp: resignedApp, certificateSerialNumber: certificateSerialNumber, storeBuildVersion: storeBuildVersion)
}
}
public extension InstalledApp
{
var localizedVersion: String {
guard let storeBuildVersion else { return self.version }
let localizedVersion = "\(self.version) (\(storeBuildVersion))"
return localizedVersion
}
func update(resignedApp: ALTApplication, certificateSerialNumber: String?, storeBuildVersion: String?)
{
self.name = resignedApp.name
self.resignedBundleIdentifier = resignedApp.bundleIdentifier
self.version = resignedApp.version
2024-12-13 15:26:11 +05:30
// TODO: @mahee96: requires altsign-marketplace branch release or equivalent
// self.buildVersion = resignedApp.buildVersion
self.storeBuildVersion = storeBuildVersion
self.certificateSerialNumber = certificateSerialNumber
if let provisioningProfile = resignedApp.provisioningProfile
{
self.update(provisioningProfile: provisioningProfile)
}
}
func update(provisioningProfile: ALTProvisioningProfile)
{
self.refreshedDate = provisioningProfile.creationDate
self.expirationDate = provisioningProfile.expirationDate
}
func loadIcon(completion: @escaping (Result<UIImage?, Error>) -> Void)
{
2024-12-07 17:45:09 +05:30
// TODO: @mahee96: Fix this later (reason: alternateIcon is not available for appEx)
// if self.bundleIdentifier == StoreApp.altstoreAppID,
// let iconName = UIApplication.alt_shared?.alternateIconName
// {
// // Use alternate app icon for AltStore, if one is chosen.
//
// let image = UIImage(named: iconName)
// completion(.success(image))
//
// return
// }
let hasAlternateIcon = self.hasAlternateIcon
let alternateIconURL = self.alternateIconURL
let fileURL = self.fileURL
DispatchQueue.global().async {
do
{
if hasAlternateIcon,
case let data = try Data(contentsOf: alternateIconURL),
let icon = UIImage(data: data)
{
return completion(.success(icon))
}
let application = ALTApplication(fileURL: fileURL)
completion(.success(application?.icon))
}
catch
{
completion(.failure(error))
}
}
}
func matches(_ appVersion: AppVersion) -> Bool
{
let matchesAppVersion = (self.version == appVersion.version && self.storeBuildVersion == appVersion.buildVersion)
return matchesAppVersion
}
}
public extension InstalledApp
{
@nonobjc class func fetchRequest() -> NSFetchRequest<InstalledApp>
{
return NSFetchRequest<InstalledApp>(entityName: "InstalledApp")
}
class func supportedUpdatesFetchRequest() -> NSFetchRequest<InstalledApp>
{
let fetchRequest = InstalledApp.fetchRequest() as NSFetchRequest<InstalledApp>
2024-12-07 17:45:09 +05:30
// let predicateFormat = [
// // isActive && storeApp != nil && latestSupportedVersion != nil
// "%K == YES AND %K != nil AND %K != nil",
2024-12-07 17:45:09 +05:30
// "AND",
2024-12-07 17:45:09 +05:30
// // latestSupportedVersion.version != installedApp.version || latestSupportedVersion.buildVersion != installedApp.storeBuildVersion
// //
// // We have to also check !(latestSupportedVersion.buildVersion == '' && installedApp.storeBuildVersion == nil)
// // because latestSupportedVersion.buildVersion stores an empty string for nil, while installedApp.storeBuildVersion uses NULL.
// "(%K != %K OR (%K != %K AND NOT (%K == '' AND %K == nil)))",
2024-12-07 17:45:09 +05:30
// "AND",
2024-12-07 17:45:09 +05:30
// // !isPledgeRequired || isPledged
// "(%K == NO OR %K == YES)"
// ].joined(separator: " ")
2024-12-07 17:45:09 +05:30
// fetchRequest.predicate = NSPredicate(format: predicateFormat,
// #keyPath(InstalledApp.isActive), #keyPath(InstalledApp.storeApp), #keyPath(InstalledApp.storeApp.latestSupportedVersion),
// #keyPath(InstalledApp.storeApp.latestSupportedVersion.version), #keyPath(InstalledApp.version),
// #keyPath(InstalledApp.storeApp.latestSupportedVersion._buildVersion), #keyPath(InstalledApp.storeBuildVersion),
// #keyPath(InstalledApp.storeApp.latestSupportedVersion._buildVersion), #keyPath(InstalledApp.storeBuildVersion),
// #keyPath(InstalledApp.storeApp.isPledgeRequired), #keyPath(InstalledApp.storeApp.isPledged))
fetchRequest.predicate = NSPredicate(format: "%K == YES AND %K == YES",
#keyPath(InstalledApp.isActive), #keyPath(InstalledApp.hasUpdate))
return fetchRequest
}
class func activeAppsFetchRequest() -> NSFetchRequest<InstalledApp>
{
let fetchRequest = InstalledApp.fetchRequest() as NSFetchRequest<InstalledApp>
fetchRequest.predicate = NSPredicate(format: "%K == YES", #keyPath(InstalledApp.isActive))
print("Active Apps Fetch Request: \(String(describing: fetchRequest.predicate))")
return fetchRequest
}
class func fetchAltStore(in context: NSManagedObjectContext) -> InstalledApp?
{
let predicate = NSPredicate(format: "%K == %@", #keyPath(InstalledApp.bundleIdentifier), StoreApp.altstoreAppID)
print("Fetch 'AltStore' Predicate: \(String(describing: predicate))")
let altStore = InstalledApp.first(satisfying: predicate, in: context)
return altStore
}
class func fetchActiveApps(in context: NSManagedObjectContext) -> [InstalledApp]
{
let activeApps = InstalledApp.fetch(InstalledApp.activeAppsFetchRequest(), in: context)
return activeApps
}
class func fetchAppsForRefreshingAll(in context: NSManagedObjectContext) -> [InstalledApp]
{
let predicate = NSPredicate(format: "(%K == YES AND %K != %@) AND (%K == nil OR %K == NO OR %K == YES)",
#keyPath(InstalledApp.isActive),
#keyPath(InstalledApp.bundleIdentifier), StoreApp.altstoreAppID,
#keyPath(InstalledApp.storeApp),
#keyPath(InstalledApp.storeApp.isPledgeRequired),
#keyPath(InstalledApp.storeApp.isPledged))
var installedApps = InstalledApp.all(satisfying: predicate,
sortedBy: [NSSortDescriptor(keyPath: \InstalledApp.expirationDate, ascending: true)],
in: context)
if let altStoreApp = InstalledApp.fetchAltStore(in: context)
{
// Refresh AltStore last since it causes app to quit.
if let storeApp = altStoreApp.storeApp
{
if !storeApp.isPledgeRequired || storeApp.isPledged
{
// Only add AltStore if it's the public version OR if it's the beta and we're pledged to it.
installedApps.append(altStoreApp)
}
}
else
{
// No associated storeApp, so add it just to be safe.
installedApps.append(altStoreApp)
}
}
return installedApps
}
class func fetchAppsForBackgroundRefresh(in context: NSManagedObjectContext) -> [InstalledApp]
{
// Date 6 hours before now.
let date = Date().addingTimeInterval(-1 * 6 * 60 * 60)
let predicate = NSPredicate(format: "(%K == YES) AND (%K < %@) AND (%K != %@) AND (%K == nil OR %K == NO OR %K == YES)",
#keyPath(InstalledApp.isActive),
#keyPath(InstalledApp.refreshedDate), date as NSDate,
#keyPath(InstalledApp.bundleIdentifier), StoreApp.altstoreAppID,
#keyPath(InstalledApp.storeApp),
#keyPath(InstalledApp.storeApp.isPledgeRequired),
#keyPath(InstalledApp.storeApp.isPledged)
)
var installedApps = InstalledApp.all(satisfying: predicate,
sortedBy: [NSSortDescriptor(keyPath: \InstalledApp.expirationDate, ascending: true)],
in: context)
if let altStoreApp = InstalledApp.fetchAltStore(in: context), altStoreApp.refreshedDate < date
{
if let storeApp = altStoreApp.storeApp
{
if !storeApp.isPledgeRequired || storeApp.isPledged
{
// Only add AltStore if it's the public version OR if it's the beta and we're pledged to it.
installedApps.append(altStoreApp)
}
}
else
{
// No associated storeApp, so add it just to be safe.
installedApps.append(altStoreApp)
}
}
return installedApps
}
}
public extension InstalledApp
{
// TODO: @mahee96: Do NOT hardcode app's url scheme prefixes as in here
// Need to get it dynamically from the Info.plist of other means
var openAppURL: URL {
let openAppURL = URL(string: "sidestore-" + self.bundleIdentifier + "://")!
return openAppURL
}
// TODO: @mahee96: Do NOT hardcode app's url scheme prefixes as in here
// Need to get it dynamically from the Info.plist of other means
class func openAppURL(for app: AppProtocol) -> URL
{
let openAppURL = URL(string: "sidestore-" + app.bundleIdentifier + "://")!
return openAppURL
}
var isUpdateAvailable: Bool {
guard let storeApp = self.storeApp, let latestVersion = storeApp.latestSupportedVersion else { return false }
guard !storeApp.isPledgeRequired || storeApp.isPledged else { return false }
let isUpdateAvailable = !self.matches(latestVersion)
return isUpdateAvailable
}
}
public extension InstalledApp
{
class var appsDirectoryURL: URL {
let baseDirectory = FileManager.default.altstoreSharedDirectory ?? FileManager.default.applicationSupportDirectory
let appsDirectoryURL = baseDirectory.appendingPathComponent("Apps")
do { try FileManager.default.createDirectory(at: appsDirectoryURL, withIntermediateDirectories: true, attributes: nil) }
catch { print("Creating App Directory Error: \(error)") }
return appsDirectoryURL
}
class var legacyAppsDirectoryURL: URL {
let baseDirectory = FileManager.default.applicationSupportDirectory
let appsDirectoryURL = baseDirectory.appendingPathComponent("Apps")
return appsDirectoryURL
}
class func fileURL(for app: AppProtocol) -> URL
{
let appURL = self.directoryURL(for: app).appendingPathComponent("App.app")
return appURL
}
class func refreshedIPAURL(for app: AppProtocol) -> URL
{
let ipaURL = self.directoryURL(for: app).appendingPathComponent("Refreshed.ipa")
print("`ipaURL`: \(ipaURL.absoluteString)")
return ipaURL
}
class func directoryURL(for app: AppProtocol) -> URL
{
let directoryURL = InstalledApp.appsDirectoryURL.appendingPathComponent(app.bundleIdentifier)
do { try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) }
catch { print(error) }
return directoryURL
}
class func installedAppUTI(forBundleIdentifier bundleIdentifier: String) -> String
{
let installedAppUTI = "io.sidestore.Installed." + bundleIdentifier
return installedAppUTI
}
class func installedBackupAppUTI(forBundleIdentifier bundleIdentifier: String) -> String
{
let installedBackupAppUTI = InstalledApp.installedAppUTI(forBundleIdentifier: bundleIdentifier) + ".backup"
return installedBackupAppUTI
}
class func alternateIconURL(for app: AppProtocol) -> URL
{
let installedBackupAppUTI = self.directoryURL(for: app).appendingPathComponent("AltIcon.png")
return installedBackupAppUTI
}
var directoryURL: URL {
return InstalledApp.directoryURL(for: self)
}
var fileURL: URL {
return InstalledApp.fileURL(for: self)
}
var refreshedIPAURL: URL {
return InstalledApp.refreshedIPAURL(for: self)
}
var installedAppUTI: String {
return InstalledApp.installedAppUTI(forBundleIdentifier: self.resignedBundleIdentifier)
}
var installedBackupAppUTI: String {
return InstalledApp.installedBackupAppUTI(forBundleIdentifier: self.resignedBundleIdentifier)
}
var alternateIconURL: URL {
return InstalledApp.alternateIconURL(for: self)
}
}