Skip to content

Commit 3a3d063

Browse files
committed
feat(ios): run iOS apps on an Apple silicon Mac with --device mac
Apple silicon Macs run iPhone and iPad apps as "Designed for iPad". @nativescript/ios supports this from NativeScript/ios#551, which loads UIKit and the other iOS-only frameworks from /System/iOSSupport. The Mac is offered as an iOS device ("My Mac (Designed for iPad)"). It is listed by `ns devices` and used only when `--device` is `mac` or the Mac's provisioning UDID, so a plain `ns run ios` never launches on it. - Build: the signed iphoneos build with destination `platform=macOS,arch=arm64,variant=Designed for iPad,id=<UDID>`, into platforms/ios/build/mac so it doesn't replace device or simulator output. - Install: the bundle goes into the wrapper layout macOS requires (<Name>.app/Wrapper/<Name>.app plus a WrappedBundle symlink), then `open -n` launches it. - Sync, logs (`log stream`) and the debugger (Darwin notification, then a local socket) work on the local file system, the same way they do for a simulator. - Warns when @nativescript/ios is older than NativeScript/ios#551.
1 parent c2ca309 commit 3a3d063

23 files changed

Lines changed: 820 additions & 9 deletions

‎lib/common/bootstrap.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ injector.require(
200200
"iOSSimulatorDiscovery",
201201
"./mobile/mobile-core/ios-simulator-discovery",
202202
);
203+
injector.require(
204+
"iOSMacDeviceDiscovery",
205+
"./mobile/mobile-core/ios-mac-device-discovery",
206+
);
203207
injector.require(
204208
"androidDeviceDiscovery",
205209
"./mobile/mobile-core/android-device-discovery",
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import * as path from "path";
2+
import { ApplicationManagerBase } from "../../application-manager-base";
3+
import { hook, sleep } from "../../../helpers";
4+
import { cache } from "../../../decorators";
5+
import { IOS_LOG_PREDICATE } from "../../../constants";
6+
import {
7+
IChildProcess,
8+
IDictionary,
9+
IFileSystem,
10+
IHooksService,
11+
IPlistParser,
12+
} from "../../../declarations";
13+
import { IOptions } from "../../../../declarations";
14+
15+
interface IIOSMacApplicationHost extends Mobile.IiOSDevice {
16+
readonly wrapperPath: string;
17+
readonly applicationBundlePath: string;
18+
readonly executableName: string;
19+
}
20+
21+
export class IOSMacApplicationManager extends ApplicationManagerBase {
22+
constructor(
23+
private device: IIOSMacApplicationHost,
24+
private $childProcess: IChildProcess,
25+
private $fs: IFileSystem,
26+
private $options: IOptions,
27+
private $plistParser: IPlistParser,
28+
protected $deviceLogProvider: Mobile.IDeviceLogProvider,
29+
$logger: ILogger,
30+
$hooksService: IHooksService,
31+
) {
32+
super($logger, $hooksService, $deviceLogProvider);
33+
}
34+
35+
public async getInstalledApplications(): Promise<string[]> {
36+
const infoPlistPath = path.join(
37+
this.device.applicationBundlePath,
38+
"Info.plist",
39+
);
40+
if (!this.$fs.exists(infoPlistPath)) {
41+
return [];
42+
}
43+
44+
const infoPlist = await this.$plistParser.parseFile(infoPlistPath);
45+
return infoPlist?.CFBundleIdentifier ? [infoPlist.CFBundleIdentifier] : [];
46+
}
47+
48+
@hook("install")
49+
public async installApplication(packageFilePath: string): Promise<void> {
50+
const wrapperPath = this.device.wrapperPath;
51+
this.$fs.deleteDirectory(wrapperPath);
52+
this.$fs.ensureDirectoryExists(path.join(wrapperPath, "Wrapper"));
53+
// ditto keeps the bundle's symlinks and extended attributes, which its
54+
// code signature covers.
55+
await this.$childProcess.spawnFromEvent(
56+
"ditto",
57+
[packageFilePath, this.device.applicationBundlePath],
58+
"close",
59+
);
60+
this.$fs.symlink(
61+
path.join("Wrapper", path.basename(this.device.applicationBundlePath)),
62+
path.join(wrapperPath, "WrappedBundle"),
63+
);
64+
}
65+
66+
public async uninstallApplication(appIdentifier: string): Promise<void> {
67+
await this.stopApplication({
68+
appId: appIdentifier,
69+
projectName: null,
70+
projectDir: null,
71+
});
72+
this.$fs.deleteDirectory(this.device.wrapperPath);
73+
}
74+
75+
public async startApplication(
76+
appData: Mobile.IStartApplicationData,
77+
): Promise<void> {
78+
await this.setDeviceLogData(appData);
79+
80+
const args = ["-n", this.device.wrapperPath];
81+
if (appData.waitForDebugger) {
82+
args.push("--args", "--nativescript-debug-brk");
83+
}
84+
await this.$childProcess.spawnFromEvent("open", args, "close");
85+
86+
const [pid] = await this.getRunningPids();
87+
if (pid) {
88+
this.$deviceLogProvider.setApplicationPidForDevice(
89+
this.device.deviceInfo.identifier,
90+
pid,
91+
);
92+
}
93+
}
94+
95+
public async stopApplication(
96+
appData: Mobile.IApplicationData,
97+
): Promise<void> {
98+
await this.device.destroyDebugSocket(appData.appId);
99+
100+
for (const signal of ["TERM", "KILL"]) {
101+
const pids = await this.getRunningPids();
102+
if (!pids.length) {
103+
return;
104+
}
105+
106+
await this.$childProcess
107+
.spawnFromEvent("kill", [`-${signal}`, ...pids], "close")
108+
.catch((err) => this.$logger.trace(err));
109+
110+
// `open -n` while the old instance is still exiting starts a second copy.
111+
for (let attempt = 0; attempt < 40; attempt++) {
112+
if (!(await this.getRunningPids()).length) {
113+
return;
114+
}
115+
await sleep(50);
116+
}
117+
}
118+
}
119+
120+
public async getDebuggableApps(): Promise<
121+
Mobile.IDeviceApplicationInformation[]
122+
> {
123+
return [];
124+
}
125+
126+
public async getDebuggableAppViews(
127+
appIdentifiers: string[],
128+
): Promise<IDictionary<Mobile.IDebugWebViewInfo[]>> {
129+
return null;
130+
}
131+
132+
/**
133+
* macOS runs the app from a copy of the wrapper under /private/var/folders,
134+
* so match the bundle layout rather than the path the CLI installed to.
135+
*/
136+
private async getRunningPids(): Promise<string[]> {
137+
const name = this.device.executableName.replace(
138+
/[.*+?^${}()|[\]\\]/g,
139+
"\\$&",
140+
);
141+
try {
142+
const result = await this.$childProcess.spawnFromEvent(
143+
"pgrep",
144+
["-f", `/Wrapper/${name}\\.app/${name}( |$)`],
145+
"close",
146+
);
147+
return result.stdout.split(/\s+/).filter(Boolean);
148+
} catch (err) {
149+
// pgrep exits with 1 when nothing matches.
150+
return [];
151+
}
152+
}
153+
154+
private async setDeviceLogData(
155+
appData: Mobile.IApplicationData,
156+
): Promise<void> {
157+
this.$deviceLogProvider.setProjectNameForDevice(
158+
this.device.deviceInfo.identifier,
159+
appData.projectName,
160+
);
161+
this.$deviceLogProvider.setProjectDirForDevice(
162+
this.device.deviceInfo.identifier,
163+
appData.projectDir,
164+
);
165+
166+
if (!this.$options.justlaunch) {
167+
await this.startDeviceLog();
168+
}
169+
}
170+
171+
@cache()
172+
private startDeviceLog(): Promise<void> {
173+
return this.device.openDeviceLogStream({ predicate: IOS_LOG_PREDICATE });
174+
}
175+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as semver from "semver";
2+
3+
/**
4+
* `--device mac` selects this Mac; it is also the device's imageIdentifier.
5+
*/
6+
export const IOS_MAC_DEVICE_ALIAS = "mac";
7+
8+
/**
9+
* Builds for the Mac go to platforms/ios/build/mac so they never mix with the
10+
* archive and .ipa of a phone build, which share the iphoneos SDK.
11+
*/
12+
export const IOS_MAC_BUILD_DIR_NAME = "mac";
13+
14+
export function isIOSMacDevice(device: Mobile.IDevice): boolean {
15+
return device?.deviceInfo?.imageIdentifier === IOS_MAC_DEVICE_ALIAS;
16+
}
17+
18+
/**
19+
* Whether this @nativescript/ios version loads UIKit from /System/iOSSupport
20+
* on a Mac (NativeScript/ios#551). Older runtimes stop at launch with
21+
* "UIApplicationDidFinishLaunchingNotification is not defined".
22+
* Returns true for versions it can't parse, so only a known-bad one warns.
23+
*/
24+
export function iOSRuntimeCanRunOnMac(version: string): boolean {
25+
const next = /^\d+\.\d+\.\d+-next\.(\d{4}-\d{2}-\d{2})-(\d+)$/.exec(
26+
version ?? "",
27+
);
28+
if (next) {
29+
const [, date, runId] = next;
30+
const firstDate = "2026-11-11";
31+
const firstRunId = 43080423883;
32+
return date > firstDate || (date === firstDate && +runId >= firstRunId);
33+
}
34+
35+
if (!semver.valid(version)) {
36+
return true;
37+
}
38+
39+
return semver.gte(version, "9.1.1");
40+
}

0 commit comments

Comments
 (0)