Skip to content

Commit 85983b7

Browse files
committed
fix(orientation): Set orientation values in Xcodeproj
Otherwise when Xcode consolidates the plist file it will probably overwrite the ones in the plist with the ones defined in the Xcodeproj file (which is where it wants to put things by default nowadays).
1 parent 849cfe1 commit 85983b7

File tree

2 files changed

+115
-35
lines changed

2 files changed

+115
-35
lines changed

lib/prepare.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,6 @@ function updateProject (platformConfig, locations) {
232232
delete infoPlist.NSAppTransportSecurity;
233233
}
234234

235-
handleOrientationSettings(platformConfig, infoPlist);
236-
237235
/* eslint-disable no-tabs */
238236
// Write out the plist file with the same formatting as Xcode does
239237
let info_contents = plist.build(infoPlist, { indent: '\t', offset: -1 });
@@ -245,27 +243,41 @@ function updateProject (platformConfig, locations) {
245243
return handleBuildSettings(platformConfig, locations, infoPlist);
246244
}
247245

248-
function handleOrientationSettings (platformConfig, infoPlist) {
246+
function handleOrientationSettings (platformConfig, project) {
247+
function setProp (name, value) {
248+
if (value) {
249+
project.xcode.updateBuildProperty(`INFOPLIST_KEY_${name}`, `"${value}"`, null, 'App');
250+
} else {
251+
project.xcode.updateBuildProperty(`INFOPLIST_KEY_${name}`, null, null, 'App');
252+
}
253+
}
254+
255+
const kPort = 'UIInterfaceOrientationPortrait';
256+
const kPortU = 'UIInterfaceOrientationPortraitUpsideDown';
257+
const kLandL = 'UIInterfaceOrientationLandscapeLeft';
258+
const kLandR = 'UIInterfaceOrientationLandscapeRight';
259+
249260
switch (getOrientationValue(platformConfig)) {
250261
case 'portrait':
251-
infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationPortrait'];
252-
infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown'];
253-
infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown'];
262+
setProp('UIInterfaceOrientation', kPort);
263+
setProp('UISupportedInterfaceOrientations_iPhone', [kPort, kPortU].join(' '));
264+
setProp('UISupportedInterfaceOrientations_iPad', [kPort, kPortU].join(' '));
254265
break;
255266
case 'landscape':
256-
infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationLandscapeLeft'];
257-
infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
258-
infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
267+
setProp('UIInterfaceOrientation', kLandL);
268+
setProp('UISupportedInterfaceOrientations_iPhone', [kLandL, kLandR].join(' '));
269+
setProp('UISupportedInterfaceOrientations_iPad', [kLandL, kLandR].join(' '));
259270
break;
260271
case 'all':
261-
infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationPortrait'];
262-
infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
263-
infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
272+
// TODO: Should we default to portrait in this case or set it to null?
273+
setProp('UIInterfaceOrientation', kPort);
274+
setProp('UISupportedInterfaceOrientations_iPhone', [kPort, kPortU, kLandL, kLandR].join(' '));
275+
setProp('UISupportedInterfaceOrientations_iPad', [kPort, kPortU, kLandL, kLandR].join(' '));
264276
break;
265277
case 'default':
266-
infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
267-
infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
268-
delete infoPlist.UIInterfaceOrientation;
278+
setProp('UIInterfaceOrientation', null);
279+
setProp('UISupportedInterfaceOrientations_iPhone', [kPort, kLandL, kLandR].join(' '));
280+
setProp('UISupportedInterfaceOrientations_iPad', [kPort, kPortU, kLandL, kLandR].join(' '));
269281
}
270282
}
271283

@@ -321,6 +333,8 @@ function handleBuildSettings (platformConfig, locations, infoPlist) {
321333
project.xcode.updateBuildProperty('SWIFT_VERSION', swiftVersion);
322334
}
323335

336+
handleOrientationSettings(platformConfig, project);
337+
324338
project.write();
325339

326340
// If we have a Podfile, we want to update the deployment target there too
@@ -1111,7 +1125,7 @@ function getOrientationValue (platformConfig) {
11111125

11121126
let orientation = platformConfig.getPreference('orientation');
11131127
if (!orientation) {
1114-
return '';
1128+
return ORIENTATION_DEFAULT;
11151129
}
11161130

11171131
orientation = orientation.toLowerCase();

tests/spec/unit/prepare.spec.js

+85-19
Original file line numberDiff line numberDiff line change
@@ -1043,55 +1043,121 @@ describe('prepare', () => {
10431043
});
10441044
it('Test#005 : should write out the orientation preference value', () => {
10451045
cfg.getPreference.and.callThrough();
1046+
writeFileSyncSpy.and.callThrough();
10461047
return updateProject(cfg, p.locations).then(() => {
1047-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown']);
1048-
expect(plist.build.calls.mostRecent().args[0]['UISupportedInterfaceOrientations~ipad']).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown']);
1049-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toEqual(['UIInterfaceOrientationPortrait']);
1048+
const proj = new XcodeProject(p.locations.pbxproj);
1049+
proj.parseSync();
1050+
1051+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1052+
expect(orientation).toEqual('"UIInterfaceOrientationPortrait"');
1053+
1054+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1055+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown"');
1056+
1057+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1058+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown"');
10501059
});
10511060
});
10521061
it('Test#006 : should handle no orientation', () => {
1053-
cfg.getPreference.and.returnValue('');
1062+
cfg.getPreference.and.returnValue(null);
1063+
writeFileSyncSpy.and.callThrough();
10541064
return updateProject(cfg, p.locations).then(() => {
1055-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toBeUndefined();
1056-
expect(plist.build.calls.mostRecent().args[0]['UISupportedInterfaceOrientations~ipad']).toBeUndefined();
1057-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toBeUndefined();
1065+
const proj = new XcodeProject(p.locations.pbxproj);
1066+
proj.parseSync();
1067+
1068+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1069+
expect(orientation).toBeUndefined();
1070+
1071+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1072+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
1073+
1074+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1075+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
10581076
});
10591077
});
10601078
it('Test#007 : should handle default orientation', () => {
10611079
cfg.getPreference.and.returnValue('default');
1080+
writeFileSyncSpy.and.callThrough();
10621081
return updateProject(cfg, p.locations).then(() => {
1063-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1064-
expect(plist.build.calls.mostRecent().args[0]['UISupportedInterfaceOrientations~ipad']).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1065-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toBeUndefined();
1082+
const proj = new XcodeProject(p.locations.pbxproj);
1083+
proj.parseSync();
1084+
1085+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1086+
expect(orientation).toBeUndefined();
1087+
1088+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1089+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
1090+
1091+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1092+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
10661093
});
10671094
});
10681095
it('Test#008 : should handle portrait orientation', () => {
10691096
cfg.getPreference.and.returnValue('portrait');
1097+
writeFileSyncSpy.and.callThrough();
10701098
return updateProject(cfg, p.locations).then(() => {
1071-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown']);
1072-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toEqual(['UIInterfaceOrientationPortrait']);
1099+
const proj = new XcodeProject(p.locations.pbxproj);
1100+
proj.parseSync();
1101+
1102+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1103+
expect(orientation).toEqual('"UIInterfaceOrientationPortrait"');
1104+
1105+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1106+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown"');
1107+
1108+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1109+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown"');
10731110
});
10741111
});
10751112
it('Test#009 : should handle landscape orientation', () => {
10761113
cfg.getPreference.and.returnValue('landscape');
1114+
writeFileSyncSpy.and.callThrough();
10771115
return updateProject(cfg, p.locations).then(() => {
1078-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1079-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toEqual(['UIInterfaceOrientationLandscapeLeft']);
1116+
const proj = new XcodeProject(p.locations.pbxproj);
1117+
proj.parseSync();
1118+
1119+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1120+
expect(orientation).toEqual('"UIInterfaceOrientationLandscapeLeft"');
1121+
1122+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1123+
expect(phone_supported).toEqual('"UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
1124+
1125+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1126+
expect(pad_supported).toEqual('"UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
10801127
});
10811128
});
10821129
it('Test#010 : should handle all orientation on ios', () => {
10831130
cfg.getPreference.and.returnValue('all');
1131+
writeFileSyncSpy.and.callThrough();
10841132
return updateProject(cfg, p.locations).then(() => {
1085-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1086-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toEqual(['UIInterfaceOrientationPortrait']);
1133+
const proj = new XcodeProject(p.locations.pbxproj);
1134+
proj.parseSync();
1135+
1136+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1137+
expect(orientation).toEqual('"UIInterfaceOrientationPortrait"');
1138+
1139+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1140+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
1141+
1142+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1143+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
10871144
});
10881145
});
10891146
it('Test#011 : should handle custom orientation', () => {
10901147
cfg.getPreference.and.returnValue('some-custom-orientation');
1148+
writeFileSyncSpy.and.callThrough();
10911149
return updateProject(cfg, p.locations).then(() => {
1092-
expect(plist.build.calls.mostRecent().args[0].UISupportedInterfaceOrientations).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1093-
expect(plist.build.calls.mostRecent().args[0]['UISupportedInterfaceOrientations~ipad']).toEqual(['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight']);
1094-
expect(plist.build.calls.mostRecent().args[0].UIInterfaceOrientation).toBeUndefined();
1150+
const proj = new XcodeProject(p.locations.pbxproj);
1151+
proj.parseSync();
1152+
1153+
const orientation = proj.getBuildProperty('INFOPLIST_KEY_UIInterfaceOrientation', undefined, 'App');
1154+
expect(orientation).toBeUndefined();
1155+
1156+
const phone_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone', undefined, 'App');
1157+
expect(phone_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
1158+
1159+
const pad_supported = proj.getBuildProperty('INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad', undefined, 'App');
1160+
expect(pad_supported).toEqual('"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"');
10951161
});
10961162
});
10971163

0 commit comments

Comments
 (0)