Skip to content

Commit ebbaff9

Browse files
thymikeeclaude
andauthored
fix(ios): recognize and answer the localized Open in launch confirmation (callstack#4160)
* fix(ios): recognize and answer the localized Open in launch confirmation On a non-English iOS simulator, the `Open in "<App>"?` confirmation that `open --launch-url` raises stayed unrecognized (`alert-unrecognized`), and `alert accept` refused its button (`Ouvrir`) with ALERT_BUTTON_NOT_FOUND. The runner now reads SpringBoard's own SchemeApproval strings (CoreServices.framework) with the strings it already reads for permission prompts. Their open and cancel labels join the alert vocabulary, so `alert accept` and `dismiss` answer the prompt in each preferred language. The runner also marks an alert read as the launch confirmation when its title and both buttons are the OS's wording of one language, and `open` accepts a marked prompt instead of matching English text. Unreadable strings recognize nothing, as before. Fixes callstack#3922 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(ios): read the launch confirmation strings apart from the permission prompts A runtime without the SchemeApproval table no longer drops the notification, tracking, and location vocabulary. The test shares the CONFIRMATION fixture. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(ios): keep the launch confirmation mark apart from open's outcome Rename the runner's alert mark to launchConfirmationPrompt so it cannot be read as open's launchConfirmation outcome, and document it. Accept positional app-name placeholders in the OS title format, skip a wording with no placeholder, and name the failing subsystem in the unavailable-strings log. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * docs: scope the launch confirmation mark to local iOS simulators Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(ios): mark only SpringBoard's modal and read bracketed title formats The launch-confirmation mark now needs the SpringBoard blocking-modal source, so an app alert with the same wording and buttons is not auto-accepted by open. The title placeholder accepts Apple's bracketed object format (Finnish `%[tt]@`). Tests load every installed SchemeApproval localization, cover the missing-table branch, and cover the app-owned alert. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * test(ios): cover Finnish in the system-strings launch confirmation test Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(ios): take only the open and cancel labels of the SchemeApproval table Another key of that undocumented table cannot join the accept or dismiss labels. The placeholder regex no longer force-unwraps. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * refactor(ios): tidy the system prompt strings docs and warm both readers apart Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * fix(ios): keep the launch confirmation labels when a permission table is unreadable The two reads are independent, so an unreadable permission table no longer discards Ouvrir and Annuler. The placeholder regex traps on a bad literal instead of silently disabling recognition. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * docs: describe the split alert string read Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --------- Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent 156d69e commit ebbaff9

13 files changed

Lines changed: 441 additions & 77 deletions

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerAlertLabelVocabulary.swift‎

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,47 @@ extension RunnerAlertLabelVocabulary {
3737
/// preferred language the app localizes into, so every preferred language takes part.
3838
static let system = fromSystemStrings(languages: Locale.preferredLanguages)
3939

40-
/// The string tables that word the system permission prompts: notifications, privacy access
41-
/// (including App Tracking Transparency), and location.
42-
private static let promptStringTables = [
43-
("/System/Library/PrivateFrameworks/UserNotificationsServer.framework", "Localizable"),
44-
("/System/Library/PrivateFrameworks/TCC.framework", "Localizable"),
45-
("/System/Library/Frameworks/CoreLocation.framework", "locationd")
46-
]
47-
48-
/// Pairs each English prompt string with the same key's value in each of `languages`. Every
49-
/// table must read, or none counts: a missing table could hide the translation that makes a
50-
/// label in another table claim both actions.
40+
/// Pairs each English prompt string with the same key's value in each of `languages`.
5141
static func fromSystemStrings(languages: [String]) -> RunnerAlertLabelVocabulary {
52-
// The simulated OS, taken from where this process's Foundation loaded; the simulator process
53-
// would otherwise read the host's /System.
54-
let foundationPath = "/System/Library/Frameworks/Foundation.framework"
55-
guard let foundation = Bundle(identifier: "com.apple.Foundation")?.bundlePath, foundation.hasSuffix(foundationPath)
56-
else { return unavailable(source: foundationPath, languages: languages) }
57-
let root = String(foundation.dropLast(foundationPath.count))
42+
composed(
43+
permission: RunnerSystemPromptStrings.read(RunnerSystemPromptStrings.promptTables, languages: languages),
44+
launchConfirmation: RunnerSystemPromptStrings.read([RunnerSystemPromptStrings.launchConfirmationTable], languages: languages),
45+
languages: languages
46+
)
47+
}
48+
49+
/// Every permission table must read, or none of them counts: a missing table could hide the
50+
/// translation that makes a label in another table claim both actions. The launch confirmation's
51+
/// table is independent of them. Only its open and cancel labels are taken, so they stay when the
52+
/// permission read fails, and the permission vocabulary stays whole when that table is missing.
53+
static func composed(
54+
permission: Result<[RunnerSystemPromptStrings], RunnerSystemPromptStrings.UnavailableSource>,
55+
launchConfirmation: Result<[RunnerSystemPromptStrings], RunnerSystemPromptStrings.UnavailableSource>,
56+
languages: [String]
57+
) -> RunnerAlertLabelVocabulary {
58+
var tables: [RunnerSystemPromptStrings] = []
59+
switch permission {
60+
case .failure(let failure):
61+
RunnerSystemPromptStrings.logUnavailable(failure, subsystem: "alert-vocabulary", languages: languages)
62+
case .success(let read):
63+
tables = read
64+
}
65+
switch launchConfirmation {
66+
case .failure(let failure):
67+
RunnerSystemPromptStrings.logUnavailable(failure, subsystem: "alert-vocabulary-launch-confirmation", languages: languages)
68+
case .success(let read):
69+
tables += read.map { $0.limited(to: RunnerSystemPromptStrings.launchConfirmationLabelKeys) }
70+
}
5871
var translations: [(english: String, localized: String)] = []
59-
for (path, table) in promptStringTables {
60-
guard let bundle = Bundle(path: root + path),
61-
let english = strings(in: bundle, table: table, localization: "en")
62-
else { return unavailable(source: path, languages: languages) }
63-
let localizations = Set(languages.compactMap {
64-
Bundle.preferredLocalizations(from: bundle.localizations, forPreferences: [$0]).first
65-
}).subtracting(["en"])
66-
for localization in localizations {
67-
guard let localized = strings(in: bundle, table: table, localization: localization)
68-
else { return unavailable(source: "\(path) \(localization)", languages: languages) }
69-
for (key, value) in english {
70-
if let translation = localized[key] { translations.append((english: value, localized: translation)) }
72+
for table in tables {
73+
for strings in table.localized.values {
74+
for (key, value) in table.english {
75+
if let translation = strings[key] { translations.append((english: value, localized: translation)) }
7176
}
7277
}
7378
}
7479
return RunnerAlertLabelVocabulary(translations: translations)
7580
}
76-
77-
private static func unavailable(source: String, languages: [String]) -> RunnerAlertLabelVocabulary {
78-
NSLog(
79-
"AGENT_DEVICE_RUNNER_ALERT_VOCABULARY_UNAVAILABLE source=%@ languages=%@",
80-
source, languages.joined(separator: ",")
81-
)
82-
return .english
83-
}
84-
85-
private static func strings(in bundle: Bundle, table: String, localization: String) -> [String: String]? {
86-
guard let url = bundle.url(forResource: table, withExtension: "strings", subdirectory: nil, localization: localization)
87-
else { return nil }
88-
return NSDictionary(contentsOf: url) as? [String: String]
89-
}
9081
}
9182
#else
9283
extension RunnerAlertLabelVocabulary {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Foundation
2+
3+
/// SpringBoard's `Open in “<App>”?` launch confirmation, recognized by the OS's own wording of its
4+
/// title and its two buttons. A prompt counts only when both buttons are exactly the open and
5+
/// cancel labels of one language, so an app alert that happens to start the same way is not it.
6+
struct RunnerLaunchConfirmationPrompt {
7+
/// One language's wording, as SpringBoard's `SchemeApproval` strings give it.
8+
struct Wording {
9+
let titleFormat: String
10+
let open: String
11+
let cancel: String
12+
}
13+
14+
static let unavailable = RunnerLaunchConfirmationPrompt(wordings: [])
15+
16+
private static let appNamePlaceholder = try! NSRegularExpression(pattern: "%(?:\\d+\\$)?(?:\\[[^\\]]*\\])?@")
17+
18+
private let wordings: [(title: NSRegularExpression, open: String, cancel: String)]
19+
20+
/// A wording whose title format has no app-name placeholder is skipped, because its pattern would
21+
/// match a title with no app name. A placeholder is `%@`, a positional `%1$@`, or Apple's
22+
/// bracketed object format `%[tt]@`, as Finnish words it.
23+
init(wordings: [Wording]) {
24+
let placeholder = Self.appNamePlaceholder
25+
self.wordings = wordings.compactMap { wording in
26+
let format = wording.titleFormat as NSString
27+
let slots = placeholder.matches(in: wording.titleFormat, range: NSRange(location: 0, length: format.length))
28+
guard !slots.isEmpty else { return nil }
29+
var pattern = ""
30+
var from = 0
31+
for slot in slots {
32+
pattern += NSRegularExpression.escapedPattern(for: format.substring(with: NSRange(location: from, length: slot.range.location - from)))
33+
pattern += ".+"
34+
from = slot.range.location + slot.range.length
35+
}
36+
pattern += NSRegularExpression.escapedPattern(for: format.substring(from: from))
37+
guard let title = try? NSRegularExpression(pattern: "^\(pattern)$") else { return nil }
38+
return (title, Self.normalized(wording.open), Self.normalized(wording.cancel))
39+
}
40+
}
41+
42+
func recognizes(title: String, buttons: [String]) -> Bool {
43+
let title = title.trimmingCharacters(in: .whitespacesAndNewlines)
44+
let titleRange = NSRange(title.startIndex..., in: title)
45+
let labels = buttons.map(Self.normalized)
46+
guard labels.count == 2 else { return false }
47+
return wordings.contains { wording in
48+
wording.title.firstMatch(in: title, range: titleRange) != nil
49+
&& Set(labels) == [wording.open, wording.cancel]
50+
}
51+
}
52+
53+
private static func normalized(_ label: String) -> String {
54+
label.trimmingCharacters(in: .whitespacesAndNewlines)
55+
}
56+
}
57+
58+
#if os(iOS) && targetEnvironment(simulator)
59+
extension RunnerLaunchConfirmationPrompt {
60+
/// English and every preferred language, read once from the OS strings. Unreadable strings
61+
/// recognize nothing, so the prompt stays an unrecognized alert.
62+
static let system = fromSystemStrings(languages: Locale.preferredLanguages)
63+
64+
static func fromSystemStrings(languages: [String]) -> RunnerLaunchConfirmationPrompt {
65+
switch RunnerSystemPromptStrings.read([RunnerSystemPromptStrings.launchConfirmationTable], languages: languages) {
66+
case .failure(let failure):
67+
RunnerSystemPromptStrings.logUnavailable(failure, subsystem: "launch-confirmation", languages: languages)
68+
return .unavailable
69+
case .success(let tables):
70+
guard let table = tables.first else { return .unavailable }
71+
let wordings = ([table.english] + table.localized.values).compactMap { strings -> Wording? in
72+
guard let title = strings["SCHEME_APPROVAL_PROMPT_TITLE_NO_SOURCE"],
73+
let open = strings["SCHEME_APPROVAL_OPEN_BUTTON"],
74+
let cancel = strings["SCHEME_APPROVAL_DO_NOT_OPEN_BUTTON"]
75+
else { return nil }
76+
return Wording(titleFormat: title, open: open, cancel: cancel)
77+
}
78+
return RunnerLaunchConfirmationPrompt(wordings: wordings)
79+
}
80+
}
81+
}
82+
#else
83+
extension RunnerLaunchConfirmationPrompt {
84+
static let system = unavailable
85+
}
86+
#endif
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import Foundation
2+
3+
#if os(iOS) && targetEnvironment(simulator)
4+
/// One OS string table read in English and in each preferred language that is not English.
5+
struct RunnerSystemPromptStrings {
6+
let english: [String: String]
7+
/// The table's strings by localization, English excluded.
8+
let localized: [String: [String: String]]
9+
10+
/// The string tables that word the system permission prompts: notifications, privacy access
11+
/// (including App Tracking Transparency), and location.
12+
static let promptTables = [
13+
(path: "/System/Library/PrivateFrameworks/UserNotificationsServer.framework", table: "Localizable"),
14+
(path: "/System/Library/PrivateFrameworks/TCC.framework", table: "Localizable"),
15+
(path: "/System/Library/Frameworks/CoreLocation.framework", table: "locationd")
16+
]
17+
18+
/// SpringBoard's launch confirmation. It is read apart from `promptTables`, so a runtime without
19+
/// it keeps the permission prompts' vocabulary.
20+
static let launchConfirmationTable = (
21+
path: "/System/Library/Frameworks/CoreServices.framework",
22+
table: "SchemeApproval"
23+
)
24+
25+
/// The only keys of the launch confirmation table that the alert vocabulary takes, so another key
26+
/// of an undocumented table cannot join the accept or dismiss labels.
27+
static let launchConfirmationLabelKeys: Set<String> = ["SCHEME_APPROVAL_OPEN_BUTTON", "SCHEME_APPROVAL_DO_NOT_OPEN_BUTTON"]
28+
29+
/// Reads `tables` from the simulated OS, or names the source that did not read. The simulated OS
30+
/// is taken from where this process's Foundation loaded; the simulator process would otherwise
31+
/// read the host's /System. A table reads in every preferred language or not at all, because a
32+
/// missing translation could hide the one that makes a label claim both actions. The read fails
33+
/// as a whole when any listed table fails, so pass tables that must stand or fall together.
34+
static func read(
35+
_ tables: [(path: String, table: String)],
36+
languages: [String]
37+
) -> Result<[RunnerSystemPromptStrings], UnavailableSource> {
38+
let foundationPath = "/System/Library/Frameworks/Foundation.framework"
39+
guard let foundation = Bundle(identifier: "com.apple.Foundation")?.bundlePath, foundation.hasSuffix(foundationPath)
40+
else { return .failure(UnavailableSource(source: foundationPath)) }
41+
let root = String(foundation.dropLast(foundationPath.count))
42+
var read: [RunnerSystemPromptStrings] = []
43+
for (path, table) in tables {
44+
guard let bundle = Bundle(path: root + path), let english = strings(in: bundle, table: table, localization: "en")
45+
else { return .failure(UnavailableSource(source: path)) }
46+
let localizations = Set(languages.compactMap {
47+
Bundle.preferredLocalizations(from: bundle.localizations, forPreferences: [$0]).first
48+
}).subtracting(["en"])
49+
var localized: [String: [String: String]] = [:]
50+
for localization in localizations {
51+
guard let strings = strings(in: bundle, table: table, localization: localization)
52+
else { return .failure(UnavailableSource(source: "\(path) \(localization)")) }
53+
localized[localization] = strings
54+
}
55+
read.append(RunnerSystemPromptStrings(english: english, localized: localized))
56+
}
57+
return .success(read)
58+
}
59+
60+
/// This table restricted to `keys`.
61+
func limited(to keys: Set<String>) -> RunnerSystemPromptStrings {
62+
RunnerSystemPromptStrings(
63+
english: english.filter { keys.contains($0.key) },
64+
localized: localized.mapValues { $0.filter { keys.contains($0.key) } }
65+
)
66+
}
67+
68+
struct UnavailableSource: Error {
69+
let source: String
70+
}
71+
72+
static func logUnavailable(_ failure: UnavailableSource, subsystem: String, languages: [String]) {
73+
NSLog(
74+
"AGENT_DEVICE_RUNNER_SYSTEM_PROMPT_STRINGS_UNAVAILABLE subsystem=%@ source=%@ languages=%@",
75+
subsystem, failure.source, languages.joined(separator: ",")
76+
)
77+
}
78+
79+
private static func strings(in bundle: Bundle, table: String, localization: String) -> [String: String]? {
80+
guard let url = bundle.url(forResource: table, withExtension: "strings", subdirectory: nil, localization: localization)
81+
else { return nil }
82+
return NSDictionary(contentsOf: url) as? [String: String]
83+
}
84+
}
85+
#endif

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Alert.swift‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,26 @@ extension RunnerTests {
137137
}
138138
}
139139

140+
let title = preferredAlertTitle(alert.root, buttons: alert.buttons)
141+
let items = alert.buttons.map { $0.label.trimmingCharacters(in: .whitespacesAndNewlines) }
140142
return Response(
141143
ok: true,
142144
data: DataPayload(
143-
message: preferredAlertTitle(alert.root, buttons: alert.buttons),
144-
items: alert.buttons.map { $0.label.trimmingCharacters(in: .whitespacesAndNewlines) }
145+
message: title,
146+
items: items,
147+
launchConfirmationPrompt: marksLaunchConfirmation(alert, title: title, items: items) ? true : nil
145148
)
146149
)
147150
}
148151

152+
/// The launch confirmation is SpringBoard's own modal. An app alert can carry the same wording
153+
/// and buttons, and `open` accepts a marked alert, so the mark needs the SpringBoard source too.
154+
func marksLaunchConfirmation(_ alert: RunnerAlert, title: String, items: [String]) -> Bool {
155+
alert.source == .blockingSystemModal
156+
&& alert.ownerApp === springboard
157+
&& RunnerLaunchConfirmationPrompt.system.recognizes(title: title, buttons: items)
158+
}
159+
149160
@MainActor
150161
func activateAlertButton(
151162
_ alert: RunnerAlert,

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Models.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ struct DataPayload: Codable {
456456
var text: String?
457457
var found: Bool?
458458
var items: [String]?
459+
/// Set on an alert read that is SpringBoard's `Open in “<App>”?` launch confirmation, in the
460+
/// device's language; absent otherwise.
461+
var launchConfirmationPrompt: Bool?
459462
var nodes: [PresentedNode]?
460463
var truncated: Bool?
461464
var qualityPayload: SnapshotQualityPayload? = nil

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ final class RunnerTests: XCTestCase {
348348
listener = try Self.makeRunnerListener(port: configuration.port)
349349
// Reads the OS prompt strings off the alert path, so the first accept/dismiss deadline does not pay for it.
350350
DispatchQueue.global(qos: .utility).async { _ = RunnerAlertLabelVocabulary.system }
351+
DispatchQueue.global(qos: .utility).async { _ = RunnerLaunchConfirmationPrompt.system }
351352
listener?.stateUpdateHandler = { [weak self] state in
352353
switch state {
353354
case .ready:

0 commit comments

Comments
 (0)