Skip to content

Commit fdca154

Browse files
Add native mobile share target support (#4021)
Co-authored-by: codex <[email protected]>
1 parent 5d68fca commit fdca154

26 files changed

Lines changed: 2343 additions & 86 deletions

‎apps/mobile/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ vp run ios:dev
3535
```
3636

3737
If your Xcode account only has a Personal Team, use a bundle identifier you control and opt into the
38-
reduced-capability local build. Personal Team builds omit the widget extension, push entitlement, and
39-
native Sign in with Apple entitlement; builds without this opt-in are unchanged.
38+
reduced-capability local build. Personal Team builds omit the widget and share extensions, push
39+
entitlement, and native Sign in with Apple entitlement; builds without this opt-in are unchanged.
4040

4141
```bash
4242
T3CODE_IOS_PERSONAL_TEAM=1 \

‎apps/mobile/app.config.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ function resolveAppVariant(value: string | undefined): AppVariant {
8484
}
8585

8686
const variant = VARIANT_CONFIG[APP_VARIANT];
87+
const iosBundleIdentifier = isIosPersonalTeamBuild
88+
? personalTeamBundleIdentifier!
89+
: variant.iosBundleIdentifier;
8790

8891
const dmSansFonts = {
8992
regular: "@expo-google-fonts/dm-sans/400Regular/DMSans_400Regular.ttf",
@@ -94,8 +97,8 @@ const dmSansFonts = {
9497
const widgetsPlugin: NonNullable<ExpoConfig["plugins"]>[number] = [
9598
"expo-widgets",
9699
{
97-
bundleIdentifier: `${variant.iosBundleIdentifier}.widgets`,
98-
groupIdentifier: `group.${variant.iosBundleIdentifier}`,
100+
bundleIdentifier: `${iosBundleIdentifier}.widgets`,
101+
groupIdentifier: `group.${iosBundleIdentifier}`,
99102
enablePushNotifications: true,
100103
// Agent activity can update many times an hour; without the
101104
// frequent-updates entitlement iOS throttles the update budget sooner.
@@ -111,6 +114,30 @@ const widgetsPlugin: NonNullable<ExpoConfig["plugins"]>[number] = [
111114
},
112115
];
113116

117+
const sharingPlugin: NonNullable<ExpoConfig["plugins"]>[number] = [
118+
"expo-sharing",
119+
{
120+
ios: {
121+
// Personal Teams cannot sign App Groups or extension targets. Keep the
122+
// reduced-capability local build usable while release builds expose the
123+
// real system share target.
124+
enabled: !isIosPersonalTeamBuild,
125+
extensionBundleIdentifier: `${iosBundleIdentifier}.sharing`,
126+
appGroupId: `group.${iosBundleIdentifier}`,
127+
activationRule: {
128+
supportsText: true,
129+
supportsWebUrlWithMaxCount: 1,
130+
supportsImageWithMaxCount: 8,
131+
},
132+
},
133+
android: {
134+
enabled: true,
135+
singleShareMimeTypes: ["text/plain", "image/*"],
136+
multipleShareMimeTypes: ["image/*"],
137+
},
138+
},
139+
];
140+
114141
// These aliases match the fonts' PostScript names on iOS. Register the same
115142
// names on Android so React Native and the native composer use one set of
116143
// family names without waiting for runtime font loading.
@@ -140,7 +167,7 @@ const config: ExpoConfig = {
140167
ios: {
141168
icon: variant.assets.iosIcon,
142169
supportsTablet: true,
143-
bundleIdentifier: variant.iosBundleIdentifier,
170+
bundleIdentifier: iosBundleIdentifier,
144171
// Pin code signing to the T3 Tools team so non-interactive `expo run:ios`
145172
// does not fall back to a personal team (which cannot sign app groups,
146173
// Sign in with Apple, or push notification entitlements).
@@ -202,6 +229,9 @@ const config: ExpoConfig = {
202229
],
203230
"expo-secure-store",
204231
"expo-sqlite",
232+
...(isIosPersonalTeamBuild
233+
? [sharingPlugin]
234+
: ["./plugins/withShareExtensionDisplayName.cjs", sharingPlugin]),
205235
[
206236
"expo-notifications",
207237
{

‎apps/mobile/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"expo-paste-input": "^0.1.15",
9292
"expo-quick-actions": "^6.0.2",
9393
"expo-secure-store": "~56.0.4",
94+
"expo-sharing": "~56.0.18",
9495
"expo-splash-screen": "~56.0.10",
9596
"expo-sqlite": "~56.0.5",
9697
"expo-symbols": "~56.0.6",
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"use strict";
2+
3+
// expo-sharing intentionally uses a fixed target name and also uses that
4+
// internal target name as CFBundleDisplayName. Brand the extension while
5+
// leaving its initially-empty signing team intact: Expo uses that state to
6+
// select ios.appleTeamId and enable first-time profile provisioning.
7+
//
8+
// ORDERING: list this plugin BEFORE expo-sharing. Expo runs same-type mods in
9+
// reverse registration order, so this executes after the extension exists.
10+
11+
const fs = require("fs");
12+
const path = require("path");
13+
const { withDangerousMod, withXcodeProject } = require("expo/config-plugins");
14+
15+
const TARGET_NAME = "expo-sharing-extension";
16+
17+
function stripComments(section) {
18+
return Object.fromEntries(
19+
Object.entries(section ?? {}).filter(([key]) => !key.endsWith("_comment")),
20+
);
21+
}
22+
23+
function findByName(section, name) {
24+
return Object.entries(stripComments(section)).find(([, value]) => value?.name === name) ?? null;
25+
}
26+
27+
function escapeXml(value) {
28+
return value
29+
.replaceAll("&", "&amp;")
30+
.replaceAll("<", "&lt;")
31+
.replaceAll(">", "&gt;")
32+
.replaceAll('"', "&quot;")
33+
.replaceAll("'", "&apos;");
34+
}
35+
36+
function withDisplayNamePlist(config, displayName) {
37+
return withDangerousMod(config, [
38+
"ios",
39+
(cfg) => {
40+
const plistPath = path.join(cfg.modRequest.platformProjectRoot, TARGET_NAME, "Info.plist");
41+
if (!fs.existsSync(plistPath)) {
42+
throw new Error(
43+
`${TARGET_NAME}/Info.plist was not generated. Register withShareExtensionDisplayName before expo-sharing.`,
44+
);
45+
}
46+
const source = fs.readFileSync(plistPath, "utf8");
47+
const displayNamePattern = /(<key>CFBundleDisplayName<\/key>\s*<string>)[^<]*(<\/string>)/;
48+
if (!displayNamePattern.test(source)) {
49+
throw new Error(`Could not update CFBundleDisplayName in ${plistPath}.`);
50+
}
51+
const next = source.replace(displayNamePattern, `$1${escapeXml(displayName)}$2`);
52+
if (next !== source) {
53+
fs.writeFileSync(plistPath, next);
54+
}
55+
return cfg;
56+
},
57+
]);
58+
}
59+
60+
function withExtensionDisplayNameBuildSetting(config, displayName) {
61+
return withXcodeProject(config, (cfg) => {
62+
const objects = cfg.modResults.hash.project.objects;
63+
const targetEntry = findByName(objects.PBXNativeTarget, TARGET_NAME);
64+
if (!targetEntry) {
65+
throw new Error(
66+
`${TARGET_NAME} Xcode target was not generated. Register withShareExtensionDisplayName before expo-sharing.`,
67+
);
68+
}
69+
const target = targetEntry[1];
70+
const configurationList = stripComments(objects.XCConfigurationList)[
71+
target.buildConfigurationList
72+
];
73+
if (!configurationList) {
74+
throw new Error(`Could not find build configurations for ${TARGET_NAME}.`);
75+
}
76+
const buildConfigurations = stripComments(objects.XCBuildConfiguration);
77+
for (const reference of configurationList.buildConfigurations ?? []) {
78+
const buildConfiguration = buildConfigurations[reference.value];
79+
if (buildConfiguration?.buildSettings) {
80+
buildConfiguration.buildSettings.INFOPLIST_KEY_CFBundleDisplayName = `"${displayName}"`;
81+
}
82+
}
83+
return cfg;
84+
});
85+
}
86+
87+
module.exports = function withShareExtensionDisplayName(config) {
88+
const displayName = config.name;
89+
return withExtensionDisplayNameBuildSetting(
90+
withDisplayNamePlist(config, displayName),
91+
displayName,
92+
);
93+
};

‎apps/mobile/src/App.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createStaticNavigation, DarkTheme, DefaultTheme } from "@react-navigati
1111
import { RegistryContext } from "@effect/atom-react";
1212
import { ConfirmDialogHost } from "./components/ConfirmDialogHost";
1313
import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
14+
import { IncomingShareProvider } from "./features/sharing/IncomingShareProvider";
1415
import { AppearancePreferencesProvider } from "./features/settings/appearance/AppearancePreferencesProvider";
1516
import { RootStack } from "./Stack";
1617
import { appAtomRegistry } from "./state/atom-registry";
@@ -26,7 +27,10 @@ const appLinking = {
2627
// <scheme>://expo-development-client/?url=<packager> — that URL addresses
2728
// the launcher, not app navigation. Without this filter it falls through
2829
// to the NotFound wildcard route on every dev launch.
29-
filter: (url: string) => !url.includes("expo-development-client"),
30+
// expo-sharing uses a private lifecycle URL only to wake the app. The
31+
// persisted share inbox below owns navigation once the payload is durable.
32+
filter: (url: string) =>
33+
!url.includes("expo-development-client") && !url.includes("://expo-sharing"),
3034
};
3135

3236
const Navigation = createStaticNavigation(RootStack);
@@ -58,10 +62,12 @@ export default function App() {
5862
the system is in dark mode. */}
5963
{/* Blur target for Android dropdown backdrops — see appBlurTarget.ts. */}
6064
<BlurTargetView ref={appBlurTargetRef} style={{ flex: 1 }}>
61-
<Navigation
62-
linking={appLinking}
63-
theme={colorScheme === "dark" ? DarkTheme : DefaultTheme}
64-
/>
65+
<IncomingShareProvider>
66+
<Navigation
67+
linking={appLinking}
68+
theme={colorScheme === "dark" ? DarkTheme : DefaultTheme}
69+
/>
70+
</IncomingShareProvider>
6571
<ConfirmDialogHost />
6672
</BlurTargetView>
6773
{/* Anchored-menu overlays render here — in-window, so the

‎apps/mobile/src/Stack.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createNativeStackScreen,
1111
type NativeStackNavigationOptions,
1212
} from "@react-navigation/native-stack";
13+
import { useEffect, useRef } from "react";
1314
import { DynamicColorIOS, Platform, Pressable, ScrollView, StyleSheet } from "react-native";
1415
import { useResolveClassNames } from "uniwind";
1516

@@ -52,6 +53,11 @@ import {
5253
SettingsLegalDocumentExternalHeaderButton,
5354
} from "./features/settings/components/SettingsLegalDocumentRouteScreen";
5455
import { useAppShortcuts } from "./features/shortcuts/useAppShortcuts";
56+
import { useIncomingShare } from "./features/sharing/IncomingShareProvider";
57+
import {
58+
EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
59+
transitionIncomingSharePresentation,
60+
} from "./features/sharing/incoming-share-presentation";
5561
import { nativeHeaderScrollEdgeEffects } from "./native/StackHeader";
5662
import { useThreadOutboxDrain } from "./state/use-thread-outbox-drain";
5763

@@ -275,12 +281,30 @@ function RootStackLayout(props: {
275281
readonly children: React.ReactNode;
276282
readonly state: NavigationState;
277283
}) {
284+
const navigation = useNavigation();
285+
const { pendingShare } = useIncomingShare();
286+
const sharePresentationRef = useRef(EMPTY_INCOMING_SHARE_PRESENTATION_STATE);
278287
useAgentNotificationNavigation();
279288
useThreadOutboxDrain();
280289
// Presents the T3 Connect onboarding sheet after an in-session sign-in.
281290
useConnectOnboardingNavigation();
282291
// Launcher app shortcuts: routes shortcut taps and tracks opened threads.
283292
useAppShortcuts(props.state);
293+
useEffect(() => {
294+
const topRouteName = props.state.routes[props.state.index]?.name;
295+
const transition = transitionIncomingSharePresentation(sharePresentationRef.current, {
296+
isShareSheetPresented: topRouteName === "NewTaskSheet",
297+
pendingShareId: pendingShare?.id ?? null,
298+
});
299+
sharePresentationRef.current = transition.state;
300+
if (!transition.shareIdToPresent) {
301+
return;
302+
}
303+
navigation.navigate("NewTaskSheet", {
304+
screen: "NewTask",
305+
params: { incomingShareId: transition.shareIdToPresent },
306+
});
307+
}, [navigation, pendingShare, props.state]);
284308
// Full pathname (sheets included) for keyboard-command scoping; the
285309
// workspace layout only reacts to the underlying non-overlay route.
286310
const path = getPathFromState(props.state, navigationPathConfig);

0 commit comments

Comments
 (0)