diff --git a/ios-app/ContentView.swift b/ios-app/ContentView.swift index 61be1a15..ff1dab88 100644 --- a/ios-app/ContentView.swift +++ b/ios-app/ContentView.swift @@ -11,8 +11,6 @@ struct ContentView: View { @Environment(\.openURL) private var openURL @State private var showSettings = false @State private var showImporter = false - /// True while the pairing-file picker is up. - @State private var showPairingImporter = false /// When false, the Advanced section under the Install button shows only its /// title. @State private var advancedExpanded = false @@ -106,13 +104,6 @@ struct ContentView: View { } .ignoresSafeArea() } - .sheet(isPresented: $showPairingImporter) { - FileImporterRepresentableView(allowedContentTypes: UTType.pairingFileTypes) { urls in - guard let url = urls.first else { return } // empty means cancelled - Task { await engine.importPairingFile(from: url) } - } - .ignoresSafeArea() - } .scrollDismissesKeyboard(.interactively) } } @@ -603,7 +594,7 @@ struct ContentView: View { } } - pairingImportButton + PairingImportButton() if engine.canGetSideStorePairing { orDivider sideStorePairingButton @@ -662,36 +653,6 @@ struct ContentView: View { .buttonStyle(.plain, sound: .tick) } - /// Import button, labelled with the imported file's name once one is in. - private var pairingImportButton: some View { - Button { showPairingImporter = true } label: { - HStack(spacing: 8) { - if engine.isImportingPairing { - ProgressView().controlSize(.small) - } else { - Image(systemName: engine.importedPairingName == nil - ? "square.and.arrow.down" : "checkmark.circle") - .contentTransition(.symbolEffect(.replace)) - .foregroundStyle(engine.importedPairingName == nil ? Color.secondary : Theme.accent) - } - Text(engine.importedPairingName ?? L("Import pairing file")) - .lineLimit(1) - .truncationMode(.middle) - .foregroundStyle(.primary) - Spacer() - if engine.importedPairingName != nil, !engine.isImportingPairing { - Text(L("Replace")) - .font(.footnote.weight(.medium)) - .foregroundStyle(Theme.accent) - } - } - .fieldBackground() - .contentShape(Rectangle()) - } - .buttonStyle(.plain, sound: .tap) - .disabled(engine.isImportingPairing) - } - // MARK: Advanced /// Advanced section under the Install button, collapsed until its title is @@ -809,7 +770,7 @@ struct ContentView: View { Text(L("(Optional)")) .font(.footnote) .foregroundStyle(.secondary) - pairingImportButton + PairingImportButton() pairingDocsLink } } @@ -1643,6 +1604,56 @@ private struct StepBadge: View { // MARK: - File picker +/// Shares the file picker and its Import / Replace control between Install and Tools. +struct PairingImportButton: View { + @Environment(Engine.self) private var engine + @EnvironmentObject private var loc: Localizer + @ObservedObject private var manager = PairingManager.shared + @State private var showPairingImporter = false + + private let reportError: ((String?) -> Void)? + + init(reportError: ((String?) -> Void)? = nil) { + self.reportError = reportError + } + + var body: some View { + Button { showPairingImporter = true } label: { + HStack(spacing: 8) { + if engine.isImportingPairing { + ProgressView().controlSize(.small) + } else { + Image(systemName: engine.importedPairingName == nil + ? "square.and.arrow.down" : "checkmark.circle") + .contentTransition(.symbolEffect(.replace)) + .foregroundStyle(engine.importedPairingName == nil ? Color.secondary : Theme.accent) + } + Text(engine.importedPairingName ?? L("Import pairing file")) + .lineLimit(1) + .truncationMode(.middle) + .foregroundStyle(.primary) + Spacer() + if engine.importedPairingName != nil, !engine.isImportingPairing { + Text(L("Replace")) + .font(.footnote.weight(.medium)) + .foregroundStyle(Theme.accent) + } + } + .fieldBackground() + .contentShape(Rectangle()) + } + .buttonStyle(.plain, sound: .tap) + .disabled(engine.isRunning || manager.isBusy || engine.isImportingPairing) + .sheet(isPresented: $showPairingImporter) { + FileImporterRepresentableView(allowedContentTypes: UTType.pairingFileTypes) { urls in + guard let url = urls.first else { return } + Task { await engine.importPairingFile(from: url, reportError: reportError) } + } + .ignoresSafeArea() + } + } +} + extension UTType { /// The `.ipa` type exported in Info.plist. Without that declaration the /// picker shows .ipa files but won't let them be selected. diff --git a/ios-app/Engine.swift b/ios-app/Engine.swift index d25b6162..f20eb000 100644 --- a/ios-app/Engine.swift +++ b/ios-app/Engine.swift @@ -3048,28 +3048,29 @@ final class Engine { /// Imports a pairing file made elsewhere (jitterbugpair, pymobiledevice3, /// idevicepair, …), replacing the one on disk. Required below iOS 27; - /// optional (under Advanced) from 27 on. + /// optional from 27 on. @MainActor - func importPairingFile(from url: URL) async { + func importPairingFile(from url: URL, reportError: ((String?) -> Void)? = nil) async { guard !isImportingPairing else { return } isImportingPairing = true // Delete the picker's temporary copy once the import is done. defer { isImportingPairing = false; Self.discardInboxCopy(url) } - lastError = nil + let setError = reportError ?? { self.lastError = $0 } + setError(nil) log("Importing pairing file \(url.lastPathComponent) …") do { let data = try Self.readImport(from: url) let kind = PairingFileKind.of(data: data) guard kind.isUsable else { - lastError = ErrorHelp.explained(L("%@ isn't a pairing file. Pick the file your computer made — a .mobiledevicepairing or .plist holding this iPhone's pair record.", - url.lastPathComponent)) - log("⛔️ Pairing import: \(lastError ?? "")") - return + throw EngineError.message(ErrorHelp.explained(L("%@ isn't a pairing file. Pick the file your computer made — a .mobiledevicepairing or .plist holding this iPhone's pair record.", + url.lastPathComponent))) } try adoptPairingFile(data, kind: kind, name: url.lastPathComponent) } catch { - lastError = L("Couldn't import %@: %@", url.lastPathComponent, error.localizedDescription) - log("⛔️ Pairing import: \(lastError ?? "")") + let message = (error as? EngineError)?.errorDescription + ?? L("Couldn't import %@: %@", url.lastPathComponent, error.localizedDescription) + log("⛔️ Pairing import: \(message)") + setError(message) } } @@ -3093,6 +3094,10 @@ final class Engine { /// and shows it under `name` as the imported file. @MainActor private func adoptPairingFile(_ data: Data, kind: PairingFileKind, name: String) throws { + guard !isRunning else { + throw EngineError.message(L("An install is already running. Wait for it to finish, then try again.")) + } + guard !PairingManager.shared.isBusy else { throw PairingController.PairingError.busy } try data.write(to: PrivateStore.pairingFile, options: .atomic) // Drop the cached merged file; it was built from the old record. CompositePairingFile.invalidateMerged() @@ -3102,6 +3107,7 @@ final class Engine { connection.disconnect() deviceSummary = nil pairingStatus = L("imported pairing file") + PairingManager.shared.pairingFileDidChange() setGuide(nil) let records = [kind.hasLockdown ? "lockdown" : nil, kind.hasRemotePairing ? "remote-pairing" : nil] diff --git a/ios-app/PairingManager.swift b/ios-app/PairingManager.swift index 3523b88e..5974517c 100644 --- a/ios-app/PairingManager.swift +++ b/ios-app/PairingManager.swift @@ -107,10 +107,15 @@ final class PairingManager: ObservableObject { private func pairThisDevice() async throws { _ = try await PairingController.shared.startAndWait() engine.connection.disconnect() + pairingFileDidChange() + } + + /// An imported or newly generated record needs a fresh scan, even if its name is unchanged. + func pairingFileDidChange() { targets = [] hasScanned = false - // Allow an auto-scan again for the new pairing file. didAutoScan = false + refresh() } // MARK: - Popups diff --git a/ios-app/PairingView.swift b/ios-app/PairingView.swift index 1198c4f0..187ccb2f 100644 --- a/ios-app/PairingView.swift +++ b/ios-app/PairingView.swift @@ -1,7 +1,7 @@ import SwiftUI import UIKit -/// The Pairing page: generate the pairing file, export it, and write it into a +/// The Pairing page: generate or import the pairing file, export it, and write it into a /// supported app installed on this iPhone over the loopback tunnel. Pushed from /// Tools, whose `NavigationStack` this relies on. struct PairingView: View { @@ -71,7 +71,7 @@ struct PairingView: View { } } - // MARK: Pairing file (generate + export) + // MARK: Pairing file private var pairingFileCard: some View { PanelCard { @@ -95,6 +95,8 @@ struct PairingView: View { .buttonStyle(PrimaryButtonStyle()) .disabled(manager.isBusy || engine.isRunning) + PairingImportButton { manager.lastError = $0 } + if let url = manager.exportURL { ShareLink(item: url) { Label(L("Export pairing file"), systemImage: "square.and.arrow.up")