Add add from file within app.

This commit is contained in:
Jeroen Leenarts 2018-09-19 16:02:20 +02:00
parent 2cd52ebd7a
commit a8796ca627
1 changed files with 39 additions and 3 deletions

View File

@ -11,6 +11,8 @@ import PromiseKit
import CoreData import CoreData
import BNRCoreDataStack import BNRCoreDataStack
import MobileCoreServices
enum AppCoordinatorError: Error { enum AppCoordinatorError: Error {
case configImportError(msg: String) case configImportError(msg: String)
} }
@ -25,6 +27,7 @@ class AppCoordinator: RootViewCoordinator {
let persistentContainer = NSPersistentContainer(name: "WireGuard") let persistentContainer = NSPersistentContainer(name: "WireGuard")
let storyboard = UIStoryboard(name: "Main", bundle: nil) let storyboard = UIStoryboard(name: "Main", bundle: nil)
var providerManagers: [NETunnelProviderManager]? var providerManagers: [NETunnelProviderManager]?
private let documentPickerDelegate: AppDocumentPickerDelegate
// MARK: - Properties // MARK: - Properties
@ -51,6 +54,9 @@ class AppCoordinator: RootViewCoordinator {
self.window.rootViewController = self.navigationController self.window.rootViewController = self.navigationController
self.window.makeKeyAndVisible() self.window.makeKeyAndVisible()
documentPickerDelegate = AppDocumentPickerDelegate()
documentPickerDelegate.appCoordinator = self
NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(self,
selector: #selector(VPNStatusDidChange(notification:)), selector: #selector(VPNStatusDidChange(notification:)),
name: .NEVPNStatusDidChange, name: .NEVPNStatusDidChange,
@ -331,17 +337,26 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate {
func addProvider(tunnelsTableViewController: TunnelsTableViewController) { func addProvider(tunnelsTableViewController: TunnelsTableViewController) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Add Manually", style: .default) { [unowned self] _ in actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Add from File", comment: ""), style: .default) { [unowned self] _ in
self.addProviderFromFile()
})
actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Add Manually", comment: ""), style: .default) { [unowned self] _ in
self.addProviderManually() self.addProviderManually()
}) })
actionSheet.addAction(UIAlertAction(title: "Scan QR Code", style: .default) { [unowned self] _ in actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Scan QR Code", comment: ""), style: .default) { [unowned self] _ in
self.addProviderWithQRScan() self.addProviderWithQRScan()
}) })
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel)) actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel))
tunnelsTableViewController.present(actionSheet, animated: true, completion: nil) tunnelsTableViewController.present(actionSheet, animated: true, completion: nil)
} }
func addProviderFromFile() {
let documentPickerController = UIDocumentPickerViewController(documentTypes: [String(kUTTypeZipArchive), "com.wireguard.config.quick"], in: .import)
documentPickerController.delegate = documentPickerDelegate
tunnelsTableViewController.present(documentPickerController, animated: true, completion: nil)
}
func addProviderManually() { func addProviderManually() {
let addContext = persistentContainer.newBackgroundContext() let addContext = persistentContainer.newBackgroundContext()
showTunnelConfigurationViewController(tunnel: nil, context: addContext) showTunnelConfigurationViewController(tunnel: nil, context: addContext)
@ -515,3 +530,24 @@ extension AppCoordinator: SettingsTableViewControllerDelegate {
self.exportConfigs(sourceView: sourceView) self.exportConfigs(sourceView: sourceView)
} }
} }
class AppDocumentPickerDelegate: NSObject, UIDocumentPickerDelegate {
weak var appCoordinator: AppCoordinator?
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if url.pathExtension == "conf" {
do {
try appCoordinator?.importConfig(config: url)
} catch {
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
}
} else if url.pathExtension == "zip" {
do {
try appCoordinator?.importConfigs(configZip: url)
} catch {
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
}
}
}
}