diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index dec0177..741e419 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 6F6899A62180447E0012E523 /* x25519.c in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899A52180447E0012E523 /* x25519.c */; }; 6F6899A8218044FC0012E523 /* Curve25519.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899A7218044FC0012E523 /* Curve25519.swift */; }; 6F6899AC218099F00012E523 /* WgQuickConfigFileParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899AB218099F00012E523 /* WgQuickConfigFileParser.swift */; }; + 6F6899B02181B07B0012E523 /* FilePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899AF2181B07A0012E523 /* FilePicker.swift */; }; 6F693A562179E556008551C1 /* Endpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F693A552179E556008551C1 /* Endpoint.swift */; }; 6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774DF217181B1006A79B3 /* MainViewController.swift */; }; 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; }; @@ -35,6 +36,7 @@ 6F6899A52180447E0012E523 /* x25519.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = x25519.c; sourceTree = ""; }; 6F6899A7218044FC0012E523 /* Curve25519.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Curve25519.swift; sourceTree = ""; }; 6F6899AB218099F00012E523 /* WgQuickConfigFileParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WgQuickConfigFileParser.swift; sourceTree = ""; }; + 6F6899AF2181B07A0012E523 /* FilePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilePicker.swift; sourceTree = ""; }; 6F693A552179E556008551C1 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = ""; }; 6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -99,6 +101,7 @@ 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */, 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */, 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */, + 6F6899AF2181B07A0012E523 /* FilePicker.swift */, ); path = iOS; sourceTree = ""; @@ -253,6 +256,7 @@ 6F7774E82172020C006A79B3 /* Configuration.swift in Sources */, 6F6899A8218044FC0012E523 /* Curve25519.swift in Sources */, 6F628C41217F47DB003482A3 /* TunnelDetailTableViewController.swift in Sources */, + 6F6899B02181B07B0012E523 /* FilePicker.swift in Sources */, 6F7774F321774263006A79B3 /* TunnelEditTableViewController.swift in Sources */, 6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */, ); diff --git a/WireGuard/WireGuard/UI/iOS/FilePicker.swift b/WireGuard/WireGuard/UI/iOS/FilePicker.swift new file mode 100644 index 0000000..25da7b9 --- /dev/null +++ b/WireGuard/WireGuard/UI/iOS/FilePicker.swift @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All rights reserved. + +import UIKit + +class FileImportViewController: UIDocumentPickerViewController { + enum DocumentType: String { + case wgQuickConfigFile = "com.wireguard.config.quick" + } + + init(documentTypes: [DocumentType]) { + super.init(documentTypes: documentTypes.map { $0.rawValue }, in: .import) + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index 08b8145..26e70ab 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -40,16 +40,21 @@ class TunnelsListTableViewController: UITableViewController { let alert = UIAlertController(title: "", message: "Add a tunnel", preferredStyle: .actionSheet) - alert.addAction( - UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in - if let s = self, let tunnelsManager = s.tunnelsManager { - s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil) - } + let importFileAction = UIAlertAction(title: "Import wg-quick config (.conf)", style: .default) { [weak self] (action) in + self?.presentViewControllerForFileImport() + } + alert.addAction(importFileAction) + + let createFromScratchAction = UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in + if let s = self, let tunnelsManager = s.tunnelsManager { + s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil) } - ) - alert.addAction( - UIAlertAction(title: "Cancel", style: .cancel) - ) + } + alert.addAction(createFromScratchAction) + + let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) + alert.addAction(cancelAction) + // popoverPresentationController will be nil on iPhone and non-nil on iPad alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem self.present(alert, animated: true, completion: nil) @@ -83,6 +88,12 @@ class TunnelsListTableViewController: UITableViewController { self.present(editNC, animated: true) } + func presentViewControllerForFileImport() { + let filePicker = FileImportViewController(documentTypes: [.wgQuickConfigFile]) + filePicker.delegate = self + self.present(filePicker, animated: true) + } + func showErrorAlert(title: String, message: String) { let okAction = UIAlertAction(title: "Ok", style: .default) let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) @@ -107,6 +118,16 @@ extension TunnelsListTableViewController: TunnelEditTableViewControllerDelegate } } +// MARK: UIDocumentPickerDelegate + +extension TunnelsListTableViewController: UIDocumentPickerDelegate { + func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { + if let url = urls.first { + openForEditing(configFileURL: url) + } + } +} + // MARK: UITableViewDataSource extension TunnelsListTableViewController {