AppDelegate.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // AppDelegate.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/14 (19/02/21 swift).
  6. // Copyright (c) 2014 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import NCCommunication
  25. import TOPasscodeViewController
  26. @UIApplicationMain
  27. class AppDelegate: UIResponder, UIApplicationDelegate, TOPasscodeViewControllerDelegate {
  28. var backgroundSessionCompletionHandler: (() -> Void)?
  29. var window: UIWindow?
  30. @objc var account: String = ""
  31. @objc var urlBase: String = ""
  32. @objc var user: String = ""
  33. @objc var userID: String = ""
  34. @objc var password: String = ""
  35. var activeFavorite: NCFavorite?
  36. var activeFiles: NCFiles?
  37. var activeFileViewInFolder: NCFileViewInFolder?
  38. var activeLogin: CCLogin?
  39. var activeLoginWeb: NCLoginWeb?
  40. @objc var activeMedia: NCMedia?
  41. var activeMore: NCMore?
  42. var activeOffline: NCOffline?
  43. var activeRecent: NCRecent?
  44. var activeServerUrl: String = ""
  45. var activeShares: NCShares?
  46. var activeTransfers: NCTransfers?
  47. var activeTrash: NCTrash?
  48. var activeViewController: UIViewController?
  49. var activeViewerVideo: NCViewerVideo?
  50. struct progressType {
  51. var progress: Float
  52. var totalBytes: Int64
  53. var totalBytesExpected: Int64
  54. }
  55. var listFilesVC: [String:NCFiles] = [:]
  56. var listFavoriteVC: [String:NCFavorite] = [:]
  57. var listOfflineVC: [String:NCOffline] = [:]
  58. var listProgress: [String:progressType] = [:]
  59. var disableSharesView: Bool = false
  60. var documentPickerViewController: NCDocumentPickerViewController?
  61. var networkingAutoUpload: NCNetworkingAutoUpload?
  62. var passcodeViewController: TOPasscodeViewController?
  63. var pasteboardOcIds: [String] = []
  64. var shares: [tableShare] = []
  65. @objc var timerErrorNetworking: Timer?
  66. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  67. // Override point for customization after application launch.
  68. return true
  69. }
  70. // L' applicazione entrerà in primo piano (attivo sempre)
  71. func applicationDidBecomeActive(_ application: UIApplication) {
  72. NCSettingsBundleHelper.setVersionAndBuildNumber()
  73. if account == "" { return}
  74. NCNetworking.shared.verifyUploadZombie()
  75. }
  76. // L' applicazione entrerà in primo piano (attivo solo dopo il background)
  77. func applicationWillEnterForeground(_ application: UIApplication) {
  78. if account == "" { return}
  79. NCCommunicationCommon.shared.writeLog("Application will enter in foreground")
  80. // Request Passcode
  81. passcodeWithAutomaticallyPromptForBiometricValidation(true)
  82. // Initialize Auto upload
  83. NCAutoUpload.shared.initAutoUpload(viewController: nil) { (_) in }
  84. // Required unsubscribing / subscribing
  85. NCPushNotification.shared().pushNotification()
  86. // Request Service Server Nextcloud
  87. NCService.shared.startRequestServicesServer()
  88. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterApplicationWillEnterForeground)
  89. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterRichdocumentGrabFocus)
  90. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterReloadDataSourceNetworkForced)
  91. }
  92. func applicationWillResignActive(_ application: UIApplication) {
  93. if account == "" { return}
  94. if activeFileViewInFolder != nil {
  95. activeFileViewInFolder?.dismiss(animated: false, completion: {
  96. self.activeFileViewInFolder = nil
  97. })
  98. }
  99. }
  100. func applicationDidEnterBackground(_ application: UIApplication) {
  101. if account == "" { return}
  102. NCCommunicationCommon.shared.writeLog("Application did enter in background")
  103. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterApplicationDidEnterBackground)
  104. passcodeWithAutomaticallyPromptForBiometricValidation(false)
  105. if #available(iOS 13.0, *) {
  106. }
  107. }
  108. func applicationWillTerminate(_ application: UIApplication) {
  109. NCCommunicationCommon.shared.writeLog("bye bye")
  110. }
  111. // MARK: -
  112. func initializeMain(notification: NSNotification) {
  113. if account == "" { return}
  114. NCCommunicationCommon.shared.writeLog("initialize Main")
  115. // Clear error certificate
  116. CCUtility.setCertificateError(account, error: false)
  117. // Registeration push notification
  118. NCPushNotification.shared().pushNotification()
  119. // Setting Theming
  120. NCBrandColor.shared.settingThemingColor(account: account)
  121. // Start Auto Upload
  122. NCAutoUpload.shared.initAutoUpload(viewController: nil) { (_) in }
  123. // Start services
  124. NCService.shared.startRequestServicesServer()
  125. // close detail
  126. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterMenuDetailClose)
  127. // Registeration domain File Provider
  128. //FileProviderDomain *fileProviderDomain = [FileProviderDomain new];
  129. //[fileProviderDomain removeAllDomains];
  130. //[fileProviderDomain registerDomains];
  131. }
  132. // MARK: - Push Notifications
  133. // MARK: - Login & checkErrorNetworking
  134. @objc func openLogin(viewController: UIViewController?, selector: Int, openLoginWeb: Bool) {
  135. }
  136. @objc func startTimerErrorNetworking() {
  137. timerErrorNetworking = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(checkErrorNetworking), userInfo: nil, repeats: true)
  138. }
  139. @objc func checkErrorNetworking() {
  140. if account == "" { return }
  141. // check unauthorized server (401)
  142. if CCUtility.getPasscode()?.count == 0 {
  143. openLogin(viewController: window?.rootViewController, selector: NCBrandGlobal.shared.introLogin, openLoginWeb: true)
  144. }
  145. // check certificate untrusted (-1202)
  146. if CCUtility.getCertificateError(account) {
  147. let alertController = UIAlertController(title: NSLocalizedString("_ssl_certificate_untrusted_", comment: ""), message: NSLocalizedString("_connect_server_anyway_", comment: ""), preferredStyle: .alert)
  148. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { action in
  149. NCNetworking.shared.writeCertificate(directoryCertificate: CCUtility.getDirectoryCerificates())
  150. self.startTimerErrorNetworking()
  151. }))
  152. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { action in
  153. self.startTimerErrorNetworking()
  154. }))
  155. window?.rootViewController?.present(alertController, animated: true, completion: {
  156. self.timerErrorNetworking?.invalidate()
  157. })
  158. }
  159. }
  160. // MARK: - Account & Communication
  161. @objc func settingAccount(_ account: String, urlBase: String, user: String, userID: String, password: String) {
  162. self.account = account
  163. self.urlBase = urlBase
  164. self.user = user
  165. self.userID = userID
  166. self.password = password
  167. _ = NCNetworkingNotificationCenter.shared
  168. NCCommunicationCommon.shared.setup(account: account, user: user, userId: userID, password: password, urlBase: urlBase)
  169. NCCommunicationCommon.shared.setup(webDav: NCUtilityFileSystem.shared.getWebDAV(account: account))
  170. NCCommunicationCommon.shared.setup(dav: NCUtilityFileSystem.shared.getDAV())
  171. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  172. if serverVersionMajor > 0 {
  173. NCCommunicationCommon.shared.setup(nextcloudVersion: serverVersionMajor)
  174. }
  175. }
  176. @objc func deleteAccount(_ account: String, wipe: Bool) {
  177. }
  178. // MARK: - Passcode & Delegate
  179. func passcodeWithAutomaticallyPromptForBiometricValidation(_ automaticallyPromptForBiometricValidation: Bool) {
  180. }
  181. }