Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Offer Reopen after granting Launchpad Developer Tools access

Lakr2336c9918e726f7
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetup.swift b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetup.swift
index 7ac5d8a7..3a6062c8 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetup.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetup.swift
@@ -50,6 +50,13 @@ final class VPhoneLaunchpadHostSetup {
]
private(set) var isChecking = false
var actionError: VPhoneLaunchpadError?
+ /// Set once Settings was opened for Developer Tools. The status the
+ /// system reports can lag the switch, so Reopen is offered from then on.
+ private(set) var didOpenDeveloperTools = false
+
+ /// Developer Tools access applies to processes launched after it is
+ /// granted. This process keeps the access it started with.
+ @ObservationIgnored private let launchDeveloperToolStatus = EPDeveloperTool().authorizationStatus
let helper: VPhoneLaunchpadHelperClient
let libraryRoot: URL
@@ -81,7 +88,18 @@ final class VPhoneLaunchpadHostSetup {
return checks.first { $0.kind == .developerTools }?.status == .passed
}
#endif
- return EPDeveloperTool().authorizationStatus == .authorized
+ return launchDeveloperToolStatus == .authorized && EPDeveloperTool().authorizationStatus == .authorized
+ }
+
+ /// False once the system reports the grant, even before a relaunch.
+ var canRequestDeveloperTools: Bool {
+ EPDeveloperTool().authorizationStatus != .authorized
+ }
+
+ /// True when a grant made after launch needs a relaunch to apply.
+ var needsRelaunch: Bool {
+ launchDeveloperToolStatus != .authorized
+ && (didOpenDeveloperTools || EPDeveloperTool().authorizationStatus == .authorized)
}
// MARK: - Checking
@@ -108,6 +126,12 @@ final class VPhoneLaunchpadHostSetup {
update(.network, await Self.network())
}
+ /// Re-reads Developer Tools access alone, for when the app comes back
+ /// from Settings.
+ func refreshDeveloperTools() {
+ update(.developerTools, developerTools())
+ }
+
private func update(_ kind: VPhoneLaunchpadHostCheck.Kind, _ result: (VPhoneLaunchpadStatus, String)) {
guard let index = checks.firstIndex(where: { $0.kind == kind }) else {
return
@@ -123,10 +147,33 @@ final class VPhoneLaunchpadHostSetup {
/// the pane is opened explicitly.
func requestDeveloperTools() async {
_ = await EPDeveloperTool().requestAccess()
+ didOpenDeveloperTools = true
update(.developerTools, developerTools())
NSWorkspace.shared.open(Self.developerToolsSettings)
}
+ /// Quits and opens Launchpad again so a new Developer Tools grant
+ /// applies. A detached waiter opens the app once this process has exited.
+ /// No machine can be in creation here: Core Bundle needs this access.
+ func relaunch() {
+ let waiter = Process()
+ waiter.executableURL = URL(fileURLWithPath: "/bin/sh")
+ waiter.arguments = [
+ "-c",
+ "while /bin/kill -0 \"$1\" 2>/dev/null; do /bin/sleep 0.2; done; /usr/bin/open \"$2\"",
+ "sh",
+ String(getpid()),
+ Bundle.main.bundlePath,
+ ]
+ do {
+ try waiter.run()
+ } catch {
+ actionError = VPhoneLaunchpadError(String(localized: "Unable to Reopen"), detail: String(localized: "Quit vphone-launchpad and open it again."))
+ return
+ }
+ NSApp.terminate(nil)
+ }
+
private static let developerToolsSettings = URL(
string: "x-apple.systempreferences:com.apple.preference.security?Privacy_DevTools",
)!
@@ -146,8 +193,10 @@ final class VPhoneLaunchpadHostSetup {
private func developerTools() -> (VPhoneLaunchpadStatus, String) {
switch EPDeveloperTool().authorizationStatus {
- case .authorized:
+ case .authorized where launchDeveloperToolStatus == .authorized:
(.passed, String(localized: "Allowed"))
+ case .authorized:
+ (.pending, String(localized: "Reopen to apply"))
case .denied:
(.failed, String(localized: "Not allowed"))
case .restricted:
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetupView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetupView.swift
index 5d492310..5b38121d 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetupView.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/HostSetup/VPhoneLaunchpadHostSetupView.swift
@@ -24,7 +24,7 @@ struct VPhoneLaunchpadHostSetupView: View {
if !host.requiredPassed {
VStack(alignment: .leading, spacing: 4) {
if host.checks.contains(where: { $0.kind == .developerTools && $0.status != .passed }) {
- Text("Allow vphone-launchpad in Privacy & Security → Developer Tools, then quit and reopen the app.")
+ Text("Allow vphone-launchpad in Privacy & Security → Developer Tools, then click Reopen.")
}
Text("Core Bundle appears once every required check passes.")
}
@@ -44,6 +44,9 @@ struct VPhoneLaunchpadHostSetupView: View {
}
}
.formStyle(.grouped)
+ .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
+ host.refreshDeveloperTools()
+ }
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
@@ -81,8 +84,17 @@ struct VPhoneLaunchpadHostSetupView: View {
private func action(for check: VPhoneLaunchpadHostCheck) -> some View {
switch check.kind {
case .developerTools where check.status != .passed:
- Button("Open Settings") {
- Task { await host.requestDeveloperTools() }
+ HStack(spacing: 8) {
+ if host.canRequestDeveloperTools {
+ Button("Open Settings") {
+ Task { await host.requestDeveloperTools() }
+ }
+ }
+ if host.needsRelaunch {
+ Button("Reopen") {
+ host.relaunch()
+ }
+ }
}
case .helper where check.status == .pending:
Button(host.helper.state == .notInstalled ? "Install…" : "Update…") {
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
index 65eb9986..52709cdc 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
@@ -761,36 +761,36 @@
}
}
},
- "Allow vphone-launchpad in Privacy & Security → Developer Tools, then quit and reopen the app." : {
+ "Allow vphone-launchpad in Privacy & Security → Developer Tools, then click Reopen." : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
- "value" : "Allow vphone-launchpad in Privacy & Security → Developer Tools, then quit and reopen the app."
+ "value" : "Allow vphone-launchpad in Privacy & Security → Developer Tools, then click Reopen."
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
- "value" : "「プライバシーとセキュリティ」→「デベロッパツール」で vphone-launchpad を許可してから、App を終了して開き直してください。"
+ "value" : "「プライバシーとセキュリティ」→「デベロッパツール」で vphone-launchpad を許可してから、「開き直す」をクリックしてください。"
}
},
"ko" : {
"stringUnit" : {
"state" : "translated",
- "value" : "개인정보 보호 및 보안 → 개발자 도구에서 vphone-launchpad를 허용한 후 앱을 종료했다가 다시 여십시오."
+ "value" : "개인정보 보호 및 보안 → 개발자 도구에서 vphone-launchpad를 허용한 후 다시 열기를 클릭하십시오."
}
},
"vi" : {
"stringUnit" : {
"state" : "translated",
- "value" : "Hãy cho phép vphone-launchpad trong Quyền riêng tư & Bảo mật → Công cụ dành cho nhà phát triển, rồi thoát và mở lại ứng dụng."
+ "value" : "Hãy cho phép vphone-launchpad trong Quyền riêng tư & Bảo mật → Công cụ dành cho nhà phát triển, rồi bấm Mở lại."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
- "value" : "请在“隐私与安全性”→“开发者工具”中允许 vphone-launchpad,然后退出并重新打开 App。"
+ "value" : "在“隐私与安全性”→“开发者工具”中允许 vphone-launchpad,然后点按“重新打开”。"
}
}
}
@@ -6077,6 +6077,40 @@
}
}
},
+ "Quit vphone-launchpad and open it again." : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Quit vphone-launchpad and open it again."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "vphone-launchpad を終了して、もう一度開いてください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "vphone-launchpad를 종료한 후 다시 여십시오."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Hãy thoát vphone-launchpad rồi mở lại."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "退出 vphone-launchpad,然后重新打开。"
+ }
+ }
+ }
+ },
"Quitting stops creating this machine. You can retry later from the step where it stopped." : {
"localizations" : {
"en" : {
@@ -6553,6 +6587,74 @@
}
}
},
+ "Reopen" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Reopen"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "開き直す"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "다시 열기"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Mở lại"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "重新打开"
+ }
+ }
+ }
+ },
+ "Reopen to apply" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Reopen to apply"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "開き直すと適用されます"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "다시 열면 적용됨"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Mở lại để áp dụng"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "重新打开后生效"
+ }
+ }
+ }
+ },
"Required" : {
"localizations" : {
"en" : {
@@ -9103,6 +9205,40 @@
}
}
},
+ "Unable to Reopen" : {
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Unable to Reopen"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "開き直せません"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "다시 열 수 없음"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Không thể mở lại"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "无法重新打开"
+ }
+ }
+ }
+ },
"Unable to Start %@" : {
"localizations" : {
"en" : {