2019-05-09 12:45:30 -07:00
|
|
|
//
|
|
|
|
|
// AppDelegate.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 5/9/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2019-06-04 18:29:50 -07:00
|
|
|
import UserNotifications
|
2019-05-09 12:45:30 -07:00
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
import AltSign
|
|
|
|
|
import Roxas
|
|
|
|
|
|
2019-05-09 12:45:30 -07:00
|
|
|
@UIApplicationMain
|
|
|
|
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
|
|
|
|
|
|
|
|
var window: UIWindow?
|
|
|
|
|
|
2019-05-20 21:24:53 +02:00
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
|
|
|
|
|
{
|
2019-05-30 17:10:50 -07:00
|
|
|
ServerManager.shared.startDiscovering()
|
|
|
|
|
|
2019-05-20 21:24:53 +02:00
|
|
|
DatabaseManager.shared.start { (error) in
|
|
|
|
|
if let error = error
|
|
|
|
|
{
|
|
|
|
|
print("Failed to start DatabaseManager.", error)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print("Started DatabaseManager")
|
2019-06-04 13:53:21 -07:00
|
|
|
|
2019-06-04 18:29:50 -07:00
|
|
|
AppManager.shared.update()
|
2019-05-20 21:24:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
if UserDefaults.standard.firstLaunch == nil
|
|
|
|
|
{
|
|
|
|
|
Keychain.shared.appleIDEmailAddress = nil
|
|
|
|
|
Keychain.shared.appleIDPassword = nil
|
|
|
|
|
Keychain.shared.signingCertificatePrivateKey = nil
|
|
|
|
|
|
|
|
|
|
UserDefaults.standard.firstLaunch = Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.prepareForBackgroundFetch()
|
2019-05-30 17:10:50 -07:00
|
|
|
|
2019-05-09 12:45:30 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
func applicationDidEnterBackground(_ application: UIApplication)
|
|
|
|
|
{
|
|
|
|
|
ServerManager.shared.stopDiscovering()
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
func applicationWillEnterForeground(_ application: UIApplication)
|
|
|
|
|
{
|
2019-06-04 18:29:50 -07:00
|
|
|
AppManager.shared.update()
|
2019-05-30 17:10:50 -07:00
|
|
|
ServerManager.shared.startDiscovering()
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
|
2019-06-04 18:29:50 -07:00
|
|
|
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
|
|
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
|
|
|
|
}
|