This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
/
IosNSBundle.ts
591 lines (533 loc) · 21.3 KB
/
IosNSBundle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import fs from 'fs-extra';
import get from 'lodash/get';
import path from 'path';
import { ExpoConfig, IOSConfig } from '@expo/config';
import * as AssetBundle from './AssetBundle';
import {
getManifestAsync,
manifestUsesSplashApi,
parseSdkMajorVersion,
saveUrlToPathAsync,
} from './ExponentTools';
import * as IosAssetArchive from './IosAssetArchive';
import * as IosIcons from './IosIcons';
// @ts-ignore: No TS support
import * as IosLaunchScreen from './IosLaunchScreen';
// @ts-ignore: No TS support
import * as IosLocalization from './IosLocalization';
import * as IosPlist from './IosPlist';
// @ts-ignore: No TS support
import * as IosWorkspace from './IosWorkspace';
import logger from './Logger';
import {
AnyStandaloneContext,
StandaloneContextService,
StandaloneContextUser,
isStandaloneContextDataService,
} from './StandaloneContext';
// TODO: move this somewhere else. this is duplicated in universe/exponent/template-files/keys,
// but xdl doesn't have access to that.
const DEFAULT_FABRIC_KEY = '81130e95ea13cd7ed9a4f455e96214902c721c99';
const DEFAULT_GAD_APPLICATION_ID = 'ca-app-pub-3940256099942544~1458002511';
const KERNEL_URL = 'https://expo.io/@exponent/home';
function _configureInfoPlistForLocalDevelopment(config: any, exp: ExpoConfig): ExpoConfig {
// add detached scheme
if (exp.isDetached && exp.detach?.scheme) {
if (!config.CFBundleURLTypes) {
config.CFBundleURLTypes = [
{
CFBundleURLSchemes: [],
},
];
}
config.CFBundleURLTypes[0].CFBundleURLSchemes.push(exp.detach.scheme);
}
// for local dev, don't specify device family here
if (config.UIDeviceFamily) {
delete config.UIDeviceFamily;
}
return config;
}
/**
* Prints warnings or info about the configured environment for local development.
*/
function _logDeveloperInfoForLocalDevelopment(infoPlist: any[]): void {
// warn about *UsageDescription changes
let usageKeysConfigured = [];
for (let key in infoPlist) {
if (key in infoPlist && key.includes('UsageDescription')) {
usageKeysConfigured.push(key);
}
}
if (usageKeysConfigured.length) {
logger.info('We added some permissions keys to `Info.plist` in your detached iOS project:');
usageKeysConfigured.forEach(key => {
logger.info(` ${key}`);
});
logger.info(
'You may want to revise them to include language appropriate to your project. You can also remove them if your app will never use the corresponding API. See the Apple docs for these keys.'
);
}
}
async function _cleanPropertyListBackupsAsync(
context: AnyStandaloneContext,
backupPath: string
): Promise<void> {
if (get(context, 'build.ios.buildType') !== 'client') {
await IosPlist.cleanBackupAsync(backupPath, 'EXShell', false);
}
await IosPlist.cleanBackupAsync(backupPath, 'Info', false);
// TODO: support this in user contexts as well
if (context instanceof StandaloneContextService) {
const { projectName } = IosWorkspace.getPaths(context);
await IosPlist.cleanBackupAsync(backupPath, `${projectName}.entitlements`, false);
}
}
/**
* Write the manifest and JS bundle to the NSBundle.
*/
async function _preloadManifestAndBundleAsync(
manifest: { bundleUrl: string; [key: string]: any },
supportingDirectory: string,
manifestFilename: string,
bundleFilename: string
): Promise<void> {
const bundleUrl = manifest.bundleUrl;
await fs.writeFile(path.join(supportingDirectory, manifestFilename), JSON.stringify(manifest));
await saveUrlToPathAsync(bundleUrl, path.join(supportingDirectory, bundleFilename));
}
/**
* This method only makes sense when operating on a context with sdk version < 26.
*/
async function _maybeLegacyPreloadKernelManifestAndBundleAsync(
context: AnyStandaloneContext,
manifestFilename: string,
bundleFilename: string
): Promise<void> {
const { supportingDirectory } = IosWorkspace.getPaths(context);
let sdkVersionSupported = await IosWorkspace.getNewestSdkVersionSupportedAsync(context);
if (parseSdkMajorVersion(sdkVersionSupported) < 26) {
logger.info('Preloading Expo kernel JS...');
const kernelManifest = await getManifestAsync(KERNEL_URL, {
'Exponent-SDK-Version': sdkVersionSupported,
'Exponent-Platform': 'ios',
Accept: 'application/expo+json,application/json',
});
return _preloadManifestAndBundleAsync(
kernelManifest,
supportingDirectory,
manifestFilename,
bundleFilename
);
}
}
/**
* Configure a standalone entitlements file.
*/
async function _configureEntitlementsAsync(context: AnyStandaloneContext): Promise<any> {
if (context instanceof StandaloneContextUser) {
// don't modify .entitlements, print info/instructions
const { exp } = context.data;
logger.info(
'Your iOS ExpoKit project will not contain an .entitlements file by default. If you need specific Apple entitlements, enable them manually via Xcode or the Apple Developer website.'
);
let keysToFlag = [];
if (exp.ios && exp.ios.usesIcloudStorage) {
keysToFlag.push('ios.usesIcloudStorage');
}
if (exp.ios && exp.ios.associatedDomains) {
keysToFlag.push('ios.associatedDomains');
}
if (keysToFlag.length) {
logger.info('We noticed the following keys in your project which may require entitlements:');
keysToFlag.forEach(key => {
logger.info(` ${key}`);
});
}
return {};
} else {
// modify the .entitlements file
const { projectName, supportingDirectory } = IosWorkspace.getPaths(context);
const manifest = isStandaloneContextDataService(context.data) && context.data.manifest;
const entitlementsFilename = `${projectName}.entitlements`;
const { appleTeamId } = context.build.ios || ({} as any);
if (!fs.existsSync(path.join(supportingDirectory, entitlementsFilename))) {
await IosPlist.createBlankAsync(supportingDirectory, entitlementsFilename);
}
const result = IosPlist.modifyAsync(supportingDirectory, entitlementsFilename, entitlements => {
// push notif entitlement changes based on build configuration
entitlements['aps-environment'] =
context.build.configuration === 'Release' ? 'production' : 'development';
// remove iCloud-specific entitlements if the developer isn't using iCloud Storage with DocumentPicker
if (manifest.ios && manifest.ios.usesIcloudStorage && appleTeamId) {
entitlements['com.apple.developer.icloud-container-identifiers'] = [
'iCloud.' + manifest.ios.bundleIdentifier,
];
entitlements['com.apple.developer.ubiquity-container-identifiers'] = [
'iCloud.' + manifest.ios.bundleIdentifier,
];
entitlements['com.apple.developer.ubiquity-kvstore-identifier'] =
appleTeamId + '.' + manifest.ios.bundleIdentifier;
entitlements['com.apple.developer.icloud-services'] = ['CloudDocuments'];
} else {
[
'com.apple.developer.icloud-container-identifiers',
'com.apple.developer.icloud-services',
'com.apple.developer.ubiquity-container-identifiers',
'com.apple.developer.ubiquity-kvstore-identifier',
].forEach(key => {
if (entitlements.hasOwnProperty(key)) {
delete entitlements[key];
}
});
}
if (manifest.ios && manifest.ios.usesAppleSignIn) {
entitlements['com.apple.developer.applesignin'] = ['Default'];
} else if (entitlements.hasOwnProperty('com.apple.developer.applesignin')) {
delete entitlements['com.apple.developer.applesignin'];
}
if (manifest.ios && manifest.ios.accessesContactNotes) {
entitlements['com.apple.developer.contacts.notes'] = manifest.ios.accessesContactNotes;
} else if (entitlements.hasOwnProperty('com.apple.developer.contacts.notes')) {
delete entitlements['com.apple.developer.contacts.notes'];
}
// Add app associated domains remove exp-specific ones.
if (manifest.ios && manifest.ios.associatedDomains) {
entitlements['com.apple.developer.associated-domains'] = manifest.ios.associatedDomains;
} else if (entitlements.hasOwnProperty('com.apple.developer.associated-domains')) {
delete entitlements['com.apple.developer.associated-domains'];
}
// for now, remove any merchant ID in shell apps
// (TODO: better plan for payments)
delete entitlements['com.apple.developer.in-app-payments'];
return entitlements;
});
return result;
}
}
/**
* Resolve the private config for a project.
* For standalone apps, this is copied into a separate context field context.data.privateConfig
* by the turtle builder. For a local project, this is available in app.json under ios.config.
*/
function _getPrivateConfig(context: AnyStandaloneContext): { [key: string]: any } | undefined {
if (context instanceof StandaloneContextService) {
return context.data.privateConfig;
} else {
const exp = context.data.exp;
if (exp && exp.ios) {
return exp.ios.config;
}
}
return undefined;
}
function _isAppleUsageDescriptionKey(key: string): boolean {
return key.includes('UsageDescription');
}
/**
* Configure an iOS Info.plist for a standalone app.
*/
async function _configureInfoPlistAsync(context: AnyStandaloneContext): Promise<void> {
const { supportingDirectory } = IosWorkspace.getPaths(context);
const config = context.config;
const privateConfig = _getPrivateConfig(context);
let result = await IosPlist.modifyAsync(supportingDirectory, 'Info', infoPlist => {
// make sure this happens first:
// apply any custom information from ios.infoPlist prior to all other Expo config
infoPlist = IOSConfig.CustomInfoPlistEntries.setCustomInfoPlistEntries(config, infoPlist);
// bundle id
let bundleIdentifier = IOSConfig.BundleIdenitifer.getBundleIdentifier(config);
if (!bundleIdentifier) {
throw new Error(`Cannot configure an iOS app with no bundle identifier.`);
}
infoPlist = IOSConfig.BundleIdenitifer.setBundleIdentifier(config, infoPlist);
// app name
infoPlist = IOSConfig.Name.setName(config, infoPlist);
if (context.build.isExpoClientBuild()) {
infoPlist = IOSConfig.Name.setDisplayName('Expo (Custom)', infoPlist);
} else {
infoPlist = IOSConfig.Name.setDisplayName(config, infoPlist);
}
// maybe set the default linking scheme
infoPlist = IOSConfig.Scheme.setScheme(config, infoPlist);
// maybe set additional linking schemes from services like fb and google
let serviceLinkingSchemes = [];
if (config.facebookScheme && config.facebookScheme.startsWith('fb')) {
serviceLinkingSchemes.push(config.facebookScheme);
}
if (
privateConfig &&
privateConfig.googleSignIn &&
privateConfig.googleSignIn.reservedClientId
) {
serviceLinkingSchemes.push(privateConfig.googleSignIn.reservedClientId);
}
// remove exp scheme, add app scheme(s)
infoPlist.CFBundleURLTypes = [
...infoPlist.CFBundleURLTypes,
{
CFBundleURLSchemes: serviceLinkingSchemes,
},
{
// Add the generic oauth redirect, it's important that it has the name
// 'OAuthRedirect' so we can find it in app code.
CFBundleURLName: 'OAuthRedirect',
CFBundleURLSchemes: [infoPlist.CFBundleIdentifier],
},
];
// add or remove other facebook config
if (config.facebookAppId) {
infoPlist.FacebookAppID = config.facebookAppId;
let queriesSchemes = infoPlist.LSApplicationQueriesSchemes || [];
queriesSchemes = queriesSchemes.concat([
'fbapi',
'fb-messenger-api',
'fbauth2',
'fbshareextension',
]);
infoPlist.LSApplicationQueriesSchemes = queriesSchemes;
} else {
delete infoPlist['FacebookAppID'];
}
if (config.facebookDisplayName) {
infoPlist.FacebookDisplayName = config.facebookDisplayName;
} else {
delete infoPlist['FacebookDisplayName'];
}
if (parseSdkMajorVersion(config.sdkVersion) >= 36) {
infoPlist.FacebookAutoInitEnabled = config.facebookAutoInitEnabled || false;
infoPlist.FacebookAutoLogAppEventsEnabled = config.facebookAutoLogAppEventsEnabled || false;
infoPlist.FacebookAdvertiserIDCollectionEnabled =
config.facebookAdvertiserIDCollectionEnabled || false;
}
// set ITSAppUsesNonExemptEncryption to let people skip manually
// entering it in iTunes Connect
if (
privateConfig &&
privateConfig.hasOwnProperty('usesNonExemptEncryption') &&
privateConfig.usesNonExemptEncryption === false
) {
infoPlist.ITSAppUsesNonExemptEncryption = false;
}
// google maps api key
if (privateConfig && privateConfig.googleMapsApiKey) {
infoPlist.GMSApiKey = privateConfig.googleMapsApiKey;
}
// Google Mobile Ads App ID
// The app crashes if the app ID isn't provided, so if the user
// doesn't provide the ID, we leave the sample one.
infoPlist.GADApplicationIdentifier =
(privateConfig && privateConfig.googleMobileAdsAppId) || DEFAULT_GAD_APPLICATION_ID;
// Auto-init of Google App Measurement
// unless the user explicitly specifies they want to auto-init, we set delay to true
infoPlist.GADDelayAppMeasurementInit = !(
privateConfig && privateConfig.googleMobileAdsAutoInit
);
// use version from manifest
infoPlist = IOSConfig.Version.setVersion(config, infoPlist);
infoPlist = IOSConfig.Version.setBuildNumber(config, infoPlist);
// NOTE(brentvatne):
// As far as I know there is no point of including an API key for Fabric on iOS?
infoPlist.Fabric = {
APIKey:
(privateConfig && privateConfig.fabric && privateConfig.fabric.apiKey) ||
DEFAULT_FABRIC_KEY,
Kits: [
{
KitInfo: {},
KitName: 'Crashlytics',
},
],
};
if (privateConfig && privateConfig.branch) {
infoPlist.branch_key = {
live: privateConfig.branch.apiKey,
};
}
// TODO(brentvatne): we will need a way in bare workflow to know what permissions are needed for iOS apps.
// We currently add usage descriptions for all of them in Expo client / standalone apps in managed workflow.
let permissionsAppName = config.name ? config.name : 'this app';
let customInfoPlistEntries = IOSConfig.CustomInfoPlistEntries.getCustomInfoPlistEntries(config);
for (let key in infoPlist) {
if (
infoPlist.hasOwnProperty(key) &&
_isAppleUsageDescriptionKey(key) &&
!customInfoPlistEntries.hasOwnProperty(key)
) {
infoPlist[key] = infoPlist[key].replace('Expo experiences', permissionsAppName);
}
}
infoPlist = IOSConfig.DeviceFamily.setDeviceFamily(config, infoPlist);
// Whether requires full screen on iPad
infoPlist.UIRequiresFullScreen = config.ios && config.ios.requireFullScreen;
if (infoPlist.UIRequiresFullScreen == null) {
// NOTES: This is defaulted to `true` for now to match the behavior prior to SDK 34, but will change to `false` in a future SDK version.
infoPlist.UIRequiresFullScreen = true;
}
// Cast to make sure that it is a boolean.
infoPlist.UIRequiresFullScreen = Boolean(infoPlist.UIRequiresFullScreen);
// Put `ios.userInterfaceStyle` into `UIUserInterfaceStyle` property of Info.plist
infoPlist = IOSConfig.UserInterfaceStyle.setUserInterfaceStyle(config, infoPlist);
// context-specific plist changes
if (context instanceof StandaloneContextUser) {
infoPlist = _configureInfoPlistForLocalDevelopment(infoPlist, context.data.exp);
_logDeveloperInfoForLocalDevelopment(infoPlist);
}
if (context instanceof StandaloneContextService && context.build.ios) {
infoPlist.CFBundleExecutable = context.build.ios.bundleExecutable;
}
return infoPlist;
});
return result;
}
/**
* Configure EXShell.plist for a standalone app.
*/
async function _configureShellPlistAsync(context: AnyStandaloneContext): Promise<void> {
const { supportingDirectory } = IosWorkspace.getPaths(context);
const config = context.config;
const buildPhaseLogger = logger.withFields({ buildPhase: 'configuring NSBundle' });
await IosPlist.modifyAsync(supportingDirectory, 'EXShell', shellPlist => {
// older SDK versions rely on `isShell` flag in xdl
if (parseSdkMajorVersion(config.sdkVersion) < 28) {
shellPlist.isShell = true;
}
shellPlist.manifestUrl = context.published.url;
shellPlist.releaseChannel = context.published.releaseChannel;
if (isStandaloneContextDataService(context.data) && context.data.testEnvironment) {
shellPlist.testEnvironment = context.data.testEnvironment;
}
if (config.ios && config.ios.permissions) {
shellPlist.permissions = config.ios.permissions;
}
if (context instanceof StandaloneContextUser) {
// disable manifest verification on detached apps until
// the developer adds the correct entitlements to their bundle id.
shellPlist.isManifestVerificationBypassed = true;
}
if (config.ios && config.ios.hasOwnProperty('isRemoteJSEnabled')) {
// deprecated, overridden by updates.enabled if that exists
shellPlist.areRemoteUpdatesEnabled = config.ios.isRemoteJSEnabled;
}
if (config.updates && config.updates.hasOwnProperty('enabled')) {
// enable/disable code push if the developer provided specific behavior
shellPlist.areRemoteUpdatesEnabled = config.updates.enabled;
}
if (!manifestUsesSplashApi(config, 'ios') && parseSdkMajorVersion(config.sdkVersion) < 28) {
// for people still using the old loading api, hide the native splash screen.
// we can remove this code eventually.
shellPlist.isSplashScreenDisabled = true;
}
buildPhaseLogger.info('Using standalone config:', shellPlist);
return shellPlist;
});
}
async function _configureConstantsPlistAsync(context: AnyStandaloneContext) {
if (context instanceof StandaloneContextUser) {
return;
}
const { supportingDirectory } = IosWorkspace.getPaths(context);
await IosPlist.modifyAsync(supportingDirectory, 'EXBuildConstants', constantsConfig => {
constantsConfig.API_SERVER_ENDPOINT =
process.env.ENVIRONMENT === 'staging'
? 'https://staging.exp.host/--/api/v2/'
: 'https://exp.host/--/api/v2/';
return constantsConfig;
});
}
async function _configureGoogleServicesPlistAsync(context: AnyStandaloneContext): Promise<void> {
if (context instanceof StandaloneContextUser) {
return;
}
if (get(context, 'data.manifest.ios.googleServicesFile')) {
const { supportingDirectory } = IosWorkspace.getPaths(context);
await fs.writeFile(
path.join(supportingDirectory, 'GoogleService-Info.plist'),
get(context, 'data.manifest.ios.googleServicesFile'),
'base64'
);
}
}
export async function configureAsync(context: AnyStandaloneContext): Promise<void> {
const buildPhaseLogger = logger.withFields({ buildPhase: 'configuring NSBundle' });
let {
intermediatesDirectory,
iosProjectDirectory,
projectName,
supportingDirectory,
} = IosWorkspace.getPaths(context);
const isExpoClientBuild = context.build.isExpoClientBuild();
if (!isExpoClientBuild && !context.published.url) {
throw new Error(`Can't configure a NSBundle without a published url.`);
}
// ensure the intermediates directory is clean of any prior build's artifacts, in the event we
// share directories across builds
await fs.remove(intermediatesDirectory);
try {
// common configuration for all contexts
buildPhaseLogger.info(`Modifying NSBundle configuration at ${supportingDirectory}...`);
await _configureInfoPlistAsync(context);
if (!isExpoClientBuild) {
await _configureShellPlistAsync(context);
}
await _configureEntitlementsAsync(context);
await _configureConstantsPlistAsync(context);
await _configureGoogleServicesPlistAsync(context);
if (!isExpoClientBuild) {
await IosLaunchScreen.configureLaunchAssetsAsync(context, intermediatesDirectory);
await IosLocalization.writeLocalizationResourcesAsync({
supportingDirectory,
context,
});
}
if (isExpoClientBuild) {
return;
}
if (context instanceof StandaloneContextUser) {
const iconPath = path.join(
iosProjectDirectory,
projectName,
'Assets.xcassets',
'AppIcon.appiconset'
);
await IosIcons.createAndWriteIconsToPathAsync(context, iconPath);
} else if (context instanceof StandaloneContextService) {
buildPhaseLogger.info('Bundling assets...');
try {
await AssetBundle.bundleAsync(
context,
context.data.manifest.bundledAssets,
supportingDirectory
);
} catch (e) {
throw new Error(`Asset bundling failed: ${e}`);
}
buildPhaseLogger.info('Compiling resources...');
await IosAssetArchive.buildAssetArchiveAsync(
context,
supportingDirectory,
intermediatesDirectory
);
await _preloadManifestAndBundleAsync(
context.data.manifest,
supportingDirectory,
'shell-app-manifest.json',
'shell-app.bundle'
);
}
await _maybeLegacyPreloadKernelManifestAndBundleAsync(
context,
'kernel-manifest.json',
'kernel.ios.bundle'
);
} catch (err) {
console.error(err);
throw err;
} finally {
buildPhaseLogger.info('Cleaning up iOS...');
await Promise.all([
_cleanPropertyListBackupsAsync(context, supportingDirectory),
fs.remove(intermediatesDirectory),
]);
}
}