2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// AppDelegate.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/6/18.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-11-10 09:29:51 +00:00
|
|
|
import TunnelKit
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
@UIApplicationMain
|
|
|
|
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
|
|
|
|
|
|
|
|
var window: UIWindow?
|
|
|
|
|
2018-10-25 07:32:55 +00:00
|
|
|
override init() {
|
2018-10-11 07:13:19 +00:00
|
|
|
AppConstants.Log.configure()
|
2018-10-25 07:32:55 +00:00
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
2018-10-11 07:13:19 +00:00
|
|
|
InfrastructureFactory.shared.loadCache()
|
2018-10-25 07:32:55 +00:00
|
|
|
Theme.current.applyAppearance()
|
2019-09-05 15:56:56 +00:00
|
|
|
Reviewer.shared.eventCountBeforeRating = AppConstants.Rating.eventCount
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
// Override point for customization after application launch.
|
|
|
|
let splitViewController = window!.rootViewController as! UISplitViewController
|
|
|
|
// splitViewController.preferredPrimaryColumnWidthFraction = 0.4
|
|
|
|
// splitViewController.minimumPrimaryColumnWidth = 360.0
|
|
|
|
splitViewController.maximumPrimaryColumnWidth = .infinity
|
|
|
|
splitViewController.delegate = self
|
|
|
|
if UI_USER_INTERFACE_IDIOM() == .pad {
|
|
|
|
splitViewController.preferredDisplayMode = .allVisible
|
|
|
|
// } else {
|
|
|
|
// splitViewController.preferredDisplayMode = .primaryOverlay
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:15:19 +00:00
|
|
|
InAppHelper.shared.requestProducts(withIdentifiers: InApp.allIdentifiers(), completionHandler: nil)
|
2019-04-06 22:36:44 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
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.
|
2018-11-04 13:35:12 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: true) // synchronize
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func applicationDidEnterBackground(_ application: UIApplication) {
|
|
|
|
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
|
|
|
|
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillEnterForeground(_ application: UIApplication) {
|
|
|
|
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillTerminate(_ application: UIApplication) {
|
|
|
|
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UISplitViewControllerDelegate
|
|
|
|
|
|
|
|
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
|
|
|
|
return !TransientStore.shared.service.hasActiveProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: URLs
|
2018-10-27 09:51:50 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
|
|
|
|
guard let root = window?.rootViewController else {
|
|
|
|
fatalError("No window.rootViewController?")
|
|
|
|
}
|
2018-10-27 18:37:30 +00:00
|
|
|
|
|
|
|
let topmost = root.presentedViewController ?? root
|
2019-03-25 19:33:07 +00:00
|
|
|
return tryParseURL(url, passphrase: nil, target: topmost)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func tryParseURL(_ url: URL, passphrase: String?, target: UIViewController) -> Bool {
|
|
|
|
let passphraseBlock = { (passphrase) in
|
|
|
|
_ = self.tryParseURL(url, passphrase: passphrase, target: target)
|
|
|
|
}
|
2019-03-25 18:44:53 +00:00
|
|
|
let passphraseCancelBlock = {
|
|
|
|
_ = try? FileManager.default.removeItem(at: url)
|
|
|
|
}
|
2019-07-08 06:44:37 +00:00
|
|
|
guard let parsingResult = OpenVPN.ConfigurationParser.Result.from(url, withErrorAlertIn: target, passphrase: passphrase, removeOnError: true, passphraseBlock: passphraseBlock, passphraseCancelBlock: passphraseCancelBlock) else {
|
2018-10-27 09:51:50 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
if let warning = parsingResult.warning {
|
2019-05-23 21:13:45 +00:00
|
|
|
OpenVPN.ConfigurationParser.Result.alertImportWarning(url: url, in: target, withWarning: warning) {
|
2018-10-27 12:40:52 +00:00
|
|
|
if $0 {
|
2019-03-25 19:33:07 +00:00
|
|
|
self.handleParsingResult(parsingResult, in: target)
|
2018-10-27 12:40:52 +00:00
|
|
|
} else {
|
2019-03-25 19:33:07 +00:00
|
|
|
try? FileManager.default.removeItem(at: url)
|
2018-10-27 12:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-03-25 19:33:07 +00:00
|
|
|
handleParsingResult(parsingResult, in: target)
|
2018-10-27 12:14:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:13:45 +00:00
|
|
|
private func handleParsingResult(_ parsingResult: OpenVPN.ConfigurationParser.Result, in target: UIViewController) {
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-27 09:51:50 +00:00
|
|
|
// already presented: update parsed configuration
|
2018-10-27 18:37:30 +00:00
|
|
|
if let nav = target as? UINavigationController, let wizard = nav.topViewController as? WizardHostViewController {
|
2018-11-10 09:29:51 +00:00
|
|
|
if let oldURL = wizard.parsingResult?.url {
|
2018-10-27 18:38:08 +00:00
|
|
|
try? FileManager.default.removeItem(at: oldURL)
|
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
wizard.parsingResult = parsingResult
|
2018-10-27 11:20:23 +00:00
|
|
|
wizard.removesConfigurationOnCancel = true
|
2018-10-27 12:14:46 +00:00
|
|
|
return
|
2018-10-27 09:51:50 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-27 09:51:50 +00:00
|
|
|
// present now
|
2018-10-27 18:37:30 +00:00
|
|
|
let wizardNav = StoryboardScene.Organizer.wizardHostIdentifier.instantiate()
|
|
|
|
guard let wizard = wizardNav.topViewController as? WizardHostViewController else {
|
2018-10-27 09:51:50 +00:00
|
|
|
fatalError("Expected WizardHostViewController from storyboard")
|
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
wizard.parsingResult = parsingResult
|
2018-10-27 11:20:23 +00:00
|
|
|
wizard.removesConfigurationOnCancel = true
|
2018-10-27 09:51:50 +00:00
|
|
|
|
2018-10-27 18:37:30 +00:00
|
|
|
wizardNav.modalPresentationStyle = .formSheet
|
|
|
|
target.present(wizardNav, animated: true, completion: nil)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UISplitViewController {
|
|
|
|
var serviceViewController: ServiceViewController? {
|
|
|
|
for vc in viewControllers {
|
|
|
|
guard let nav = vc as? UINavigationController else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if let found = nav.viewControllers.first(where: {
|
|
|
|
$0 as? ServiceViewController != nil
|
|
|
|
}) as? ServiceViewController {
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 07:42:58 +00:00
|
|
|
|
|
|
|
extension AppDelegate {
|
|
|
|
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
|
|
|
|
guard let interaction = userActivity.interaction else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if #available(iOS 12, *) {
|
2019-03-18 16:08:54 +00:00
|
|
|
IntentDispatcher.handleInteraction(interaction, completionHandler: nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|