[AltStore] Refreshes installed apps in background

This commit is contained in:
Riley Testut
2019-06-04 18:29:50 -07:00
parent 2c3e9ad615
commit a09d6d3502
30 changed files with 5058 additions and 74 deletions

View File

@@ -7,6 +7,7 @@
//
import UIKit
import UserNotifications
import AltSign
import Roxas
@@ -29,18 +30,24 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
{
print("Started DatabaseManager")
AppManager.shared.refresh()
AppManager.shared.update()
}
}
if UserDefaults.standard.firstLaunch == nil
{
Keychain.shared.appleIDEmailAddress = nil
Keychain.shared.appleIDPassword = nil
Keychain.shared.signingCertificatePrivateKey = nil
UserDefaults.standard.firstLaunch = Date()
}
self.prepareForBackgroundFetch()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication)
{
ServerManager.shared.stopDiscovering()
@@ -48,15 +55,68 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillEnterForeground(_ application: UIApplication)
{
AppManager.shared.refresh()
AppManager.shared.update()
ServerManager.shared.startDiscovering()
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
extension AppDelegate
{
private func prepareForBackgroundFetch()
{
// Fetch every 6 hours.
UIApplication.shared.setMinimumBackgroundFetchInterval(60 * 60 * 6)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
ServerManager.shared.startDiscovering()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
AppManager.shared.refreshAllApps() { (result) in
ServerManager.shared.stopDiscovering()
let content = UNMutableNotificationContent()
do
{
let results = try result.get()
for (_, result) in results
{
guard case let .failure(error) = result else { continue }
throw error
}
print(results)
content.title = "Refreshed Apps!"
content.body = "Successfully refreshed all apps."
completionHandler(.newData)
}
catch
{
print("Failed to refresh apps in background.", error)
content.title = "Failed to Refresh Apps"
content.body = error.localizedDescription
completionHandler(.failed)
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: "RefreshedApps", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print(error)
}
}
}
}
}
}