Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 52 additions & 41 deletions ios-app/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -603,7 +594,7 @@ struct ContentView: View {
}
}

pairingImportButton
PairingImportButton()
if engine.canGetSideStorePairing {
orDivider
sideStorePairingButton
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -809,7 +770,7 @@ struct ContentView: View {
Text(L("(Optional)"))
.font(.footnote)
.foregroundStyle(.secondary)
pairingImportButton
PairingImportButton()
pairingDocsLink
}
}
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 15 additions & 9 deletions ios-app/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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()
Expand All @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion ios-app/PairingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions ios-app/PairingView.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -71,7 +71,7 @@ struct PairingView: View {
}
}

// MARK: Pairing file (generate + export)
// MARK: Pairing file

private var pairingFileCard: some View {
PanelCard {
Expand All @@ -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")
Expand Down